TeAmEr
TeAmEr

Reputation: 4773

Jquery .attr converts & to & in my links !

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 &amp; .

Any clues ?

Upvotes: 1

Views: 3016

Answers (3)

Manish Trivedi
Manish Trivedi

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 &amp;

Upvotes: 0

SLaks
SLaks

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 &amp; 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

Idris Mokhtarzada
Idris Mokhtarzada

Reputation: 1044

&amp; 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

Related Questions