jquery - Unable to send id to javascript function -


i have iframe want display when hyperlink clicked. iframe has specific id i'm using selector grab. when specify iframe's id in javascript function works fine, when attempt pass id function call in hyperlink no response.

this works

<a href="#" onclick="expandmap();"><h3>map 1</h3></a> <iframe id="myiframe" data-src="https://map.embed.url" width="100%;"  height="700px" src="about:blank"> </iframe>  <script> function expandmap() {    map = $("#myiframe");    map.attr("src", map.data("src")); } </script> 

this not

<a href="#" onclick="expandmap("#myiframe");"><h3>map 1</h3></a> <iframe id="myiframe" data-src="https://map.embed.url" width="100%;"  height="700px" src="about:blank"> </iframe>  <script> function expandmap(id) {    map = $(id);    map.attr("src", map.data("src")); } </script> 

is there i'm doing wrong?

you need check quotes. can't have un-escaped double quote inside double quotes. can use single quotes:

<a href="#" onclick="expandmap('#myiframe');"> 

p.s. suggest not using inline javascript event handlers. personally, this:

<a href="#" class="expandmap" data-frame="#myiframe"><h3>map 1</h3></a> <iframe id="myiframe" data-src="https://map.embed.url" width="100%;"  height="700px" src="about:blank"> </iframe>  <script> $(function(){     $('.expandmap').click(function(){         var frame = $(this).data('frame');         map = $(frame);         map.attr("src", map.data("src"));     }); }); </script> 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -