简体   繁体   中英

how to pass dynamic anchor tag id value or class value to another script

when we fetch the data from database then we generate a dynamic anchor tag.how can we pass the dynamic generated anchor tag id to another script for further process..i have tried by still m not getting any output.below is my code:

script

<script type="text/javascript">

$(document).ready( function()
{   
    $('.c').click(function()
    {                       
        $.ajax({
            url:"<?php echo base_url(); ?>/afc/search",
            data:{},
            type:"POST",
            cache:false,
            success:function(data)
            {
                //alert(data);

                var obj = $.parseJSON(data);
                var result = "<ul>";
                    $.each(obj, function()
                    {
                        //alert(this['course_name']);
                        result = result + "<li> <a class='cours' cid='"+this['course_id']+"' bid='"+this['branch_id']+"' href='#'>" + this['course_name'] + "</a></li>";    
                    });
                    result = result + "</ul>";
                    document.getElementById("cour").innerHTML =result;

            }
        });
    });
});

</script>

<script type="text/javascript">

    $(document).ready(function () {
        $('.cours').click(function(){
            var idAttr = $(this).attr('cid');
            alert(idAttr);
        });
    });
</script>

Move the second script to a function (and out of document ready), which uses the cous selector to first unbind the click event, and then re-apply, it. Then you can call the function after you have added the result.

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