i'm new to jquery
i generated html div with php and give each them a unique id
<div id=div1>text</div>
<div id=div2>text</div>
<div id=div3>text</div>
...
the problem is, how I select specific div with jquery? usually i use this code to select an element
$('#id')
but I have to make alot of selector because each id is unique. Recently I used something like this
<div ... onclick=getElem(id)></div>
function getElem(id){
var elem=$('#div'+id);
}
Is it good to use onclick
?
Add a class to the group of divs
<div id="div1" class="someClass">text</div>
<div id="div2" class="someClass">text</div>
<div id="div3" class="someClass">text</div>
...
Then in javascript:
$('.someClass').click( function(event) {
var ele = $(this);
// ele is the element you clicked on
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments