Hardy Hardy - 2 months ago 6
HTML Question

how to select id which is dynamically change in jquery?

here is my html:



<?php echo ($row['status'] == 1)? ?>
<span id='slide_".$row['id']."' name="slid_id" class='label label-danger'>Inactive</span>
<?php : ?>
<span id='slide_".$row['id']."' name="slid_id" class='label label-success'>Active</span>
<?php ; ?>





here is my jquery code:



$(document).ready(function()
{
$("span").click(function()
{

var id = $(":input[name=slide_id]").val();
alert(id);
});
});





i didn't get anything , i want to get the


ID & its dynamic generated value


of the span element and then perform some action like on click i want to change the status from active to inactive or vise versa.

thanks in advance.

Answer

Your HTML is missing from the question, but eliminating the colon in your jQuery selector might do the trick in selecting an input field - i.e. $("input[name=slide_id]").val().

If you want to get the ID attribute of a span element, this would work:

$('span_selector').attr('id');