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='' />"; Quote 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!'; Quote 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\' />'; Quote 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" />'; Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.