jquery - passing varibles into j-query from php as attributes -
i trying pass attribute location jquery unable 1 point out on m doing wrong
php
<?php echo "<a class='classloader' location='$array[$i]' href='#'>$array[$i]</a>"; ?> jquery
<script type ="text/javascript"> $(document).ready(function () { var location= $(this).attr('location'); $(".classloader").click( function () { display(); } ); }); function display() { $('#contenthere').html(location); } </script>
you're using $(this) outside click handler, doesn't refer element clicked on.
then should pass string argument display().
$(".classloader").click(function() { var location = $(this).attr("location"); display(location); }); function display(str) { $('#contenthere').html(str); } btw, shouldn't make own attributes that, might conflict future html versions. use data-xxx attributes, reserved programmers.
<?php echo "<a class='classloader' data-location='$array[$i]' href='#'>$array[$i]</a>"; ?> then in jquery can use $(this).data('location') access it.
Comments
Post a Comment