Using php variable in jQuery Script -
i trying code work cannot figure out how make work correctly. think close though. code works fine on page:
var entryid = $(this).attr('id'); if ("<?php echo $locations[2]['floor']; ?>" == 'firstfloor' ) { $("#drawtable").attr("style", "background:url(\'images/firstfloor.jpg\') 50% / 100% no-repeat;"); } else if ("<?php echo $locations[2]['floor']; ?>" == 'secondfloor' ) { $("#drawtable").attr("style", "background:url(\'images/secondfloor.jpg\') 50% / 100% no-repeat;"); }
i trying make array changeable using entryid instead of number '2'. not think concatenating correctly below. if can appreciated! thank you
var entryid = $(this).attr('id'); if ("<?php echo $locations . "[";?>" +entryid+ "<?php echo "]['floor']"; ?>" == 'firstfloor' ) { $("#drawtable").attr("style", "background:url(\'images/firstfloor.jpg\') 50% / 100% no-repeat;"); } else if ("<?php echo $locations . "[";?>" +entryid+ "<?php echo "]['floor']"; ?>" == 'secondfloor' ) { $("#drawtable").attr("style", "background:url(\'images/secondfloor.jpg\') 50% / 100% no-repeat;"); }
then thing can't php ran on server , javascript has started work way later after dom ready. so, not possible such way. instead can store1 array in js variable like:
var entryid = $(this).attr('id'); var locations = <?php echo json_encode($locations); ?>; // suppose array. var floor = locations[entryid]['floor']; // <----target floor here if (floor === 'firstfloor') { $("#drawtable").css("background", "('images/firstfloor.jpg') 50% 100% no-repeat;"); } else if (floor === 'secondfloor') { $("#drawtable").css("background", "url('images/secondfloor.jpg') 50% 100% no-repeat;"); }
and instead of attr
such way better use css
or event better addclass
.
1. - js has written on php page.
Comments
Post a Comment