views:

128

answers:

2

I have the following code, written using jQuery:

var strval = '22'
$("#quicknote").attr("href", 
  "javascript:location.href=/'http://site.com/notes/add?projects=/'+ strval")

Which results in the following attribute:

<a href="javascript:location.href='http://site.com/notes/add?projects='+'strval'"
   id="quicknote">

But, what I want is:

<a href="javascript:location.href='http://site.com/notes/add?projects='+'22'"
   id="quicknote">

Any jQuery wizards know how I might achieve this result?

+5  A: 

Try:

var strval = "22";
$("#quicknote").attr("href",
 "javascript:location.href='http://site.com/notes/add?projects=" + strval + "'");

Note the position and type of quotes.

On a side note, I'm not exactly sure why you wouldn't to do this instead:

var strval = "22";
$("#quicknote").attr("href",
  "http://site.com/notes/add?projects=" + strval + "'");

ie theres no need for Javascript in your example.

Lastly, since you are using jQuery anyway I wouldn't put Javascript in the href like this either. Instead add a click() handler:

$("#quicknote").click(function() {
  window.location = "http://site.com/notes/add?projects=22";
  return false;
});
cletus
well done. Thank you!
AnApprentice
A: 

A JQUERY wizard tells you, that it is possible, but it has just nothing to do with jQuery, and that your answer is:

You have the quote wrong!

And there is no reason to vote me down, because I answered the initial question in very, very, very professional style.

frunsi