Target iframe with links using jquery not working -
why not work? know can use target need jquery.
<html> <head> <script src="http://code.jquery.com/jquery.min.js"></script> </head> <body> <a href="test1.html" onclick="test()">test 1</a> <br> <a href="test2.html" onclick="test()">test 2</a> <p><iframe id="viewer" src="" frameborder="1" style="width:400px; height:600px"></iframe> <script> function test() { var goto = $('this').attr('href'); $('#viewer').attr('src', goto); } </script> </body> </html>
problem code $('this')
looking html element doesn't exits.
for immediate problem, pass element using test(this)
<a href="test1.html" onclick="test(this)">test 1</a>
script
function test(elem) { $('#viewer').attr('src', elem.href); }
as using jquery bind event using it
//document ready handler executes when dom complety loaded $(function(){ //bind event $('a').on('click', function(e){ //cancel default action e.preventdefault(); //here refers element invoked event handler var goto = $(this).attr('href'); //this.href can used $('#viewer').attr('src', goto); }); });
html
<a href="test1.html">test 1</a> <a href="test2.html">test 2</a>
Comments
Post a Comment