2

So I've learned that I can use a button to link someone to another page like this:

$("#something").append("<br><center><input type='button' value='Go' onclick='location.href=\"http://www.google.com\"'></center>"); 

But let's say I wanted to use a variable as the address, I'm not sure how to do that. What I'm trying to do is something like this:

var url = "http://www.google.com"; $("#something").append("<br><center><input type='button' value='Go' onclick='location.href=url'></center>"); 

Can someone please correct my error? Thanks

1
  • Is this really a <center> tag that I see ? use text-align:center or width + margin:auto instead Commented Jan 2, 2013 at 15:47

1 Answer 1

5

You can concatenate url variable in the string like this,

Live Demo

$("#something").append("<br><center><input type='button' value='Go' onclick='location.href=" +url+ "'></center>"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Are you missing the escaped quotes (\") around the url?
still seems to not be working... I think it is because I'm missing the escaped quotes, where would I add these?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.