简体   繁体   中英

How do I call a defined variable with jQuery?

I'm kinda stuck.

I defined three variables:

var part1 = "<form><input type='button' value='Open Window' onclick='window.open('http://jsfiddle.net/
";
var part2="5Xket,"
var part3 = "'toolbar=no',menubar='no')></form>";

My aim is to concat those variables to create a working link when clicking on the button.

This is my try to concat the values of the variables.

var mytest_2=part1+part2+part3;
alert (mytest_2);

By clicking a button the button from mytest2 should appear. Clicking on that button there should be opened a new window with the url http://jsfiddle.net/5Xket/

$('#searchbutton').click(function() {
$("<td class='testclass'><input id='an_id' type='text'></td>").show();
$(mytest_2).insertAfter('#an_id');

Well, the button appears as it should, but won't open a window. My guess is that I'm wrong with the syntax somewhere because the alert puts out the correct order of variables.

Any ideas? Thank you.

I should do this way:

var part1 = '<form><input id="button1" type="button" value="Open Window" /></form>';

$(part1).insertAfter('#an_id');

$('#button1').click(function(){
   window.open('http://jsfiddle.net/','5Xket','toolbar=no,menubar=no');
});

The problem with your code is double/single quotes aren't properly escaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM