Krisalay
Krisalay

Reputation: 75

Dynamically creation of labels using jQuery and HTML and accessing their text value using jQuery

I am creating a label at run time using jQuery. The code is :

var value="Hello";
$("#ID_listOfTopics").append('<label id="ID_item">' + value + '</label><br /><hr />');

Now I don't know how to access the text value of this particular label created at run time.

Upvotes: 0

Views: 3052

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You can access to it normally using id selector #ID_item like following :

$("#ID_item").text();

For the click you have ro use on() when you want to deal with fresh DOM added by javascript.


var value="Hello";
$("#ID_listOfTopics").append('<label id="ID_item">' + value + '</label><br /><hr />');


$("#ID_listOfTopics").on('click','#ID_item', function(){ 
  alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ID_listOfTopics"></div>

Hope this helps.

Upvotes: 2

Related Questions