Hi everyone, i need your help)
i have some elements with same id, say:

<div id="myid">Data1</div>
<div id="myid">Data2</div>

I want to get innerHtml when one of them is clicked, how can i determine which one exactly was clicked?

JQuery:

$("#myid").click(function(){
//how to get the .html and other properties of clicked element?
});

Thnx

Recommended Answers

All 5 Replies

Ids must be unique, so you will need to change the id of at least one of them or you will likely experience cross-browser incompatibilities. However, it is OK to have multiple elements with the same class name. So, if you change those id='myid' to class='myid' then use $('.myid:eq(0)').html() to get the innerHTML of the first div. For the second div, change the zero to a ONE.

Ok, thank you. However, i still have a question: how to get the inner html or other property of the element that was just clicked?
For example i have a menu, or some table and the elements of this table have same class but dynamically given different ids. Now i want to get the id value when mouse is clicked on any element of the table. How can i do it? is there anything like this[/COLOR.html()]?

use the attr() method:
http://api.jquery.com/attr/

If you have
<div id="myid1" class="myid">Data1</div>
<div id="myid2" class="myid">Data2</div>


alert( $('.myid:eq(0)').attr('id') );

To get the innerhtml of the clicked item you only need:

$(".myid").click(function(){
 this.innerhtml();
});

The keyword "this" refers to the element that triggered the event.

Thank you, guys!))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.