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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -