erme Posted June 8, 2010 Share Posted June 8, 2010 Hi, How can I add html tags to a js var without it messing up. Its for a hide show jquery script: var showText="Click here to expand <img src='/images/arrow-down.png' style='float:right' alt='' /> "; var hideText="Return to top <img src='/images/arrow-up.png' style='float:right' alt='' />"; Link to comment https://forums.phpfreaks.com/topic/204166-html-tag-in-js-var/ Share on other sites More sharing options...
Adam Posted June 8, 2010 Share Posted June 8, 2010 For a start I'd use single quotes to make it easier on yourself: var showText='Click here to expand <img src="/images/arrow-down.png" style="float:right" alt="" /> '; var hideText='Return to top <img src="/images/arrow-up.png" style="float:right" alt="" />'; Then in the event you need to use a single quote, just escape it with a backslash: var str = 'it\'s that simple!'; Link to comment https://forums.phpfreaks.com/topic/204166-html-tag-in-js-var/#findComment-1069345 Share on other sites More sharing options...
erme Posted June 8, 2010 Author Share Posted June 8, 2010 So like this? var showText='Click here to expand <img src=\'/images/arrow-down.png\' /> '; var hideText='Return to top <img src=\'/images/arrow-up.png\' />'; Link to comment https://forums.phpfreaks.com/topic/204166-html-tag-in-js-var/#findComment-1069354 Share on other sites More sharing options...
Adam Posted June 8, 2010 Share Posted June 8, 2010 Think you're missing the point in using single quotes, it's to make writing HTML (with correct double quotes around attribute values) easier. For your example you can just use: var showText='Click here to expand <img src="/images/arrow-down.png" /> '; var hideText='Return to top <img src="/images/arrow-up.png" />'; Link to comment https://forums.phpfreaks.com/topic/204166-html-tag-in-js-var/#findComment-1069355 Share on other sites More sharing options...
brianlange Posted June 8, 2010 Share Posted June 8, 2010 Use the jQuery html function to assign to assign the value of the variable to the element. Link to comment https://forums.phpfreaks.com/topic/204166-html-tag-in-js-var/#findComment-1069579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.