g2902249 g2902249 - 1 year ago 91
PHP Question

passing varibles into j-query from php as attributes

i am trying to pass the attribute

into jquery but unable to do so
could some one point out on what m i doing wrong


<?php echo "<a class='classloader' location='$array[$i]' href='#'>$array[$i]</a>"; ?>


<script type ="text/javascript">
$(document).ready(function () {
var location= $(this).attr('location');
function () {

function display() {


Answer Source

You're using $(this) outside the click handler, so it doesn't refer to the element that they clicked on.

Then you should pass the string as an argument to display().

$(".classloader").click(function() {
    var location = $(this).attr("location");

function display(str) {

BTW, you shouldn't make up your own attributes like that, they might conflict with future HTML versions. Use data-XXX attributes, which are reserved for programmers.

<?php echo "<a class='classloader' data-location='$array[$i]' href='#'>$array[$i]</a>";  ?>

Then in jQuery you can use $(this).data('location') to access it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download