Reputation: 4773
Am forced to use jquery and am not familiar with it .
Anyway , i store a link in a span <span id="link">http://x.com?x=1&x=2</span>
when looking up through the js file that gets that variable and puts it in href
of a link using this code $j("#sn_title").attr('href', news_link.html());
, the &
in the link is converted to &
.
Any clues ?
Upvotes: 1
Views: 3016
Reputation: 3559
The .html() is changing the & to an html & (&) but using .attr("href") sets the link path .
Please see this link
jQuery html() and &
Upvotes: 0
Reputation: 887459
The html()
method returns the actual HTML of the element.
&
is a special character in HTML (it's used for entities) and cannot appear as a normal character.
Your HTML is invalid (&x
is not an entity), but the browser automatically corrects it to &
as it is parsed.
jQuery faithfully returns the fixed-up HTML when you ask it to.
If you want the text of the element, call .text()
.
Upvotes: 2
Reputation: 1044
&
is actually acceptable (and preferred) for links per the HTML spec.
However, if you want to make it not change what you have in news_link, you should be doing: $j("#sn_title").attr('href', news_link.text());
Notice the use of .text()
instead of .html()
Upvotes: 3