简体   繁体   中英

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.

var id = $(":input[name=slide_id]").attr('id'); should do the job. .val() only returns the current value of input elements.

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

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

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

okay finally i got the way to solve this things. m just sharing this if anyone have same issue one day then this might helpful.

html codes:

<tr class="tr">
    <td class="td">
        <div id="status">
            <?php

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

my jquery code:

$(document).ready(function()
    {
        $("#status > span") .click(function()
        {

            var id = $(this).attr('id');
            var tempelement = $(this);
            var texts = $(this).closest(".tr").find("#texts").val();
            var author = $(this).closest(".tr").find("#author").val();
            var status = $(this).text();
            $.ajax({
                    type: 'POST',
                    url: 'manage_feedback.php',
                    data: {id:id, texts:texts, author:author, status:status},
                    success: function(data)
                    {
                        if (data == "Active")
                        {
                            $(tempelement).removeClass("label label-danger");
                            $(tempelement).addClass("label label-success");
                            $(tempelement).html(data);
                            alert('status changed');
                        }
                        else if (data == "Inactive")
                        {
                            $(tempelement).removeClass("label label-success");
                            $(tempelement).addClass("label label-danger");
                            $(tempelement).html(data);
                            alert('status changed');
                        }
                        else
                        {
                            alert(data);
                        }
                    }
            });
        });

php script

 //ajax status changer code if (isset($_POST['id']) && isset($_POST['texts']) && isset($_POST['status']) && isset($_POST['author'])) { $id = $_POST['id']; $texts = trim($_POST['texts']); $author = trim($_POST['author']); $status = $_POST['status']; $qry = "SELECT count(id) as count FROM tbl_testimonials WHERE texts = '".$texts."' AND author = '".$author."' AND id != ".$id." "; $sql = mysql_query($qry); $data = mysql_fetch_assoc($sql); if ($data['count'] == 0) { if($status == 'Inactive') { $qry = "UPDATE tbl_testimonials SET status = 0 WHERE id = ".$id." " ; $sql = mysql_query($qry); if($sql == 1) { echo 'Active'; exit; } } elseif ($status == 'Active') { $qry = "UPDATE tbl_testimonials SET status = 1 WHERE id = ".$id." " ; $sql = mysql_query($qry); if($sql == 1) { echo 'Inactive'; exit; } } } else { echo "name already taken"; exit; } }

hope it will help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM