javascript - Changing image in HTML using jquery when clicking on a li object -
i attempting change image displayed when user clicks on list object. if click on omega 001 change picture , text displayed oh his. have:
<?php session_start(); ?> <html> <head> <link rel="stylesheet" href="styles.css" type="text/css"> <script src="jquery-2.2.0.min.js"></script> </head> <body> <div id="header"> <h1>the bodak</h1> </div> <ul> <li><a href="history.php">back</a></li> <li><a href="#" class="link" id="link" data-title="bounties/hape.jpg" data-bio="">hape atete</a></li> <li><a href="#" class="link" id="link2" data-title="bounties/porsi.jpg" data-bio="">porsi skastrek</a></li> <li><a href="#" class="link" id="link3" data-title="bounties/alfrekr.jpg" data-bio="">alfrekr reistr</a></li> <li><a href="#" class="link" id="link4" data-title="bounties/brann.jpg" data-bio="">brann pust</a></li> <li><a href="#" class="link" id="link5" data-title="bounties/alpha.jpg" data-bio="">44a61 alpha</a></li> <li><a href="#" class="link" id="link6" data-title="bounties/omega.jpg" data-bio="">omega 001</a></li> <li><a href="#" class="link" id="link6" data-title="bounties/bidayatan.jpg" data-bio="">biayatan88b</a></li> </ul><br><br><br><br> <img src="bounties/hape.jpg" id="pic" name="pic" alt="bounty" style="width:70%;height:100%;"> <div id="bio">an assigned bounty</div><br> <script> $(document).ready(function() { $('.link').on('click', function() { var $el = $(this); $(#pic).attr("src", text($el.data('title'))); $("#bio").text($el.data('bio')); }); }); </script> </body> </html>
i working off
javascript - changing image variables
and going off question asked before got working:
selector
should wrapped inquotes
. there methodtext
defined in script , not think need any.
note: data-bio
empty in li
elements hence text
of #bio
empty.
try this:
$(document).ready(function() { $('.link').on('click', function(e) { e.preventdefault(); var $el = $(this); alert($el.data('title')); $("#pic").attr("src", $el.data('title')); $("#bio").text($el.data('bio')); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="header"> <h1>the bodak</h1> </div> <ul> <li><a href="history.php">back</a> </li> <li><a href="#" class="link" id="link" data-title="bounties/hape.jpg" data-bio="">hape atete</a> </li> <li><a href="#" class="link" id="link2" data-title="bounties/porsi.jpg" data-bio="">porsi skastrek</a> </li> <li><a href="#" class="link" id="link3" data-title="bounties/alfrekr.jpg" data-bio="">alfrekr reistr</a> </li> <li><a href="#" class="link" id="link4" data-title="bounties/brann.jpg" data-bio="">brann pust</a> </li> <li><a href="#" class="link" id="link5" data-title="bounties/alpha.jpg" data-bio="">44a61 alpha</a> </li> <li><a href="#" class="link" id="link6" data-title="bounties/omega.jpg" data-bio="">omega 001</a> </li> <li><a href="#" class="link" id="link6" data-title="bounties/bidayatan.jpg" data-bio="">biayatan88b</a> </li> </ul> <br> <br> <br> <br> <img src="bounties/hape.jpg" id="pic" name="pic" alt="bounty" style="width:70%;height:100%;"> <div id="bio">an assigned bounty</div> <br>
Comments
Post a Comment