admin管理员组文章数量:1355522
I have multiple divs added dynamically. The class name is given as "result_"+id where the id is from database table.
<div class="result_137">Gaurab Dahal</div>
<div class="result_138">saurab dahal</div>
How can I write the click event so that it can adddress the div on which it is clicked.
for example I can write the css like below to address all the divs that has classname starting with the string "result".
div[class^="result"]{
padding:5px;
width:490px;
background: rgba(204,204,204,0.5);
font: 12px arial, verdana, sans-serif;
}
I have multiple divs added dynamically. The class name is given as "result_"+id where the id is from database table.
<div class="result_137">Gaurab Dahal</div>
<div class="result_138">saurab dahal</div>
How can I write the click event so that it can adddress the div on which it is clicked.
for example I can write the css like below to address all the divs that has classname starting with the string "result".
div[class^="result"]{
padding:5px;
width:490px;
background: rgba(204,204,204,0.5);
font: 12px arial, verdana, sans-serif;
}
Share
Improve this question
asked Dec 26, 2012 at 7:27
GaurabDahalGaurabDahal
8766 silver badges16 bronze badges
3 Answers
Reset to default 5For dynamically added div you need to us on() for binding event. You can delegate event to doucment or to parent element of dynamically added elements.
Live Demo
$(document).on('click', 'div[class^=result]', function(){
alert($(this).text());
});
As you have dynamically added divs so:
$(function(){ // ready handler required
$(document).on('click', 'div[class^=result]', function(){ // .on in your case
alert($(this).text()); // will be helpful
});
});
You can get id of div like this var selection = $(ev.target).attr("id");
where ev is event and then u can apply click function of jquery like this $(selection).live('click',function(){//ur code here });
本文标签: javascriptjquery click event for a class starting with same initial nameStack Overflow
版权声明:本文标题:javascript - jquery click event for a class starting with same initial name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743954782a2567936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论