Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get id value from dynamically created div

Tags:

html

jquery

<div id="tag<%=count++%>" value="val1">
<div id="tag<%=count++%>" value="val2">
<div id="tag<%=count++%>" value="val3">

Onclick event in jquery need to get value of the div. Exact number of divs created is dynamic

like image 437
Sivadinesh Avatar asked Dec 02 '22 17:12

Sivadinesh


1 Answers

You should use input instead of div:

Try this:

Html :

<input type="text" id="tag<%=count++%>" value="val1"/>
<input type="text" id="tag<%=count++%>" value="val2"/>
<input type="text" id="tag<%=count++%>" value="val3"/>

Jquery:

$( document ).ready(function() {
 $('input[id^="tag"]').on('click', function() {  
    alert(this.value);
 });
});

Live demo

using div:

try this:

$( document ).ready(function() {
 $('div[id^="tag"]').on('click', function() {  
    alert($(this).attr('value'));
 });
});

demo

like image 178
Awlad Liton Avatar answered Dec 18 '22 16:12

Awlad Liton