jQuery variable undefined with php include -
i trying use variable in jquery wich included php, attr rel of img in footer.
i try use change attr src in same img in footer.
if use without include, works. include doesn't.
i ../img/undefined.svg result.
how can ../img/facebook.svg or ../img/twitter.svg ?
in php-file use:
<?php include '../include/header.php';?> <?php include '../include/footer.php';?>
in header.php javascript, jquery , html:
<meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="../css/stijl.css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("footer img").hover( function(){ var src = $(this).attr('rel'); $(this).attr('src', '../img/' + src + '-n.svg'); } , function(){ var src = $(this).attr("rel"); $(this).attr('src', '../img/' + src + '.svg'); } ); }); </script>
in footer.php html:
<footer> <div class="content"> <a rel="twitter" href="#"> <img src="../img/twitter.svg"> </a> <a rel="facebook" href="#"> <img src="../img/facebook.svg"> </a> </div> </footer>
$(this)
refers img
tag, has no rel
attribute. parent tag, however, does. try:
var src = $(this).parent().attr('rel');
or if markup may change slightly, may more robust:
var src = $(this).closest('a').attr('rel');
Comments
Post a Comment