views:

58

answers:

2

Hi. I am trying to add some additional html to a div with id slideshow using jQuery. My code:

$("#slideshow").append("<a id="prev" title="Previous Slide">Previous Slide</a><a id="next" title="Next Slide">Next Slide</a>");

This isn't working, can anyone tell me what I am doing wrong?

+9  A: 

You're mixing quotes.

Your string goes from the first " to the second ", meaning that the string only contains "<a id=". The string is followed by the identifier prev, then another string, creating a syntax error.

Change the outer quotes around the string to ', like this:

'<a id="prev" title="Previous Slide">Previous Slide</a><a id="next" title="Next Slide">Next Slide</a>'
SLaks
Ahhh I always wondered about that. I have seen both types used before. so I should always use ' and not " like this $('#slides') and not $("slides")?
mtwallet
I like to use the single quotes in js, it seems more readable - then if you're appending in html you can stick with the double quotes mostly.
James Westgate
That's a matter of personal preference. Just don't use both in the same line of code or else they'll conflict.
Zack
In Javascript (unlike other languages), there is no difference whatsoever between `'Hi!'` and `"Hi!"`. (Except for which quotes need to be escaped)
SLaks
Just make sure what you're using for outer quotes is different from what you're using for inner quotes. If you have inner-inner quotes then you'll probably need to escape them and it gets real ugly.
Davy8
I understand, thanks for the help guys.
mtwallet
@mtwallet: You should always use one or the other...it is personal preference. I tend towards this: `"<div></div>"` and `"<div class='foo bar'>David's div</div>"`. Where the double quotes are used on the outside and single quotes inside the string.
David Murdoch
Then you should accept this answer.
SLaks
A: 

your not escaping your quotes best way to fix is to put the append in single quotes

$("#slideshow").append('<a id="prev" title="Previous Slide">Previous Slide</a><a id="next" title="Next Slide">Next Slide</a>');
mcgrailm