0

This may be obvious to someone. I am trying to place a textbox on the screen under a specific circumstance and I am using the following code:

document.getElementById('tbDetails').innerHTML = '<textarea name="taComments" style="height:40px;width:333px" class="f1 greytext" onblur="sb_textarea_onblur(this, \'Check Out Rhapsody Tours\'s Video:\')" onfocus="sb_textarea_onfocus(this, \'Check Out Rhapsody Tours\'s Video:\')">Check Out Rhapsody Tours's Video:</textarea>';

as you can see the text has a apostrophe "Check Out Rhapsody Tours's Video:" which is causing an error. The text is created dynamically in classic asp so there is no way to tell which information will be displayed.

Other code that may help:

function sb_textarea_onfocus(txt,defaultText)
{
     if(txt&&txt.value == defaultText)
     {
         txt.value = '';
         txt.className = 'f1';
     }
}

function sb_textarea_onblur(txt,defaultText)
{
     if(txt&&txt.value == '')
     {
          txt.value = defaultText;
          txt.className = 'f1 greytext';
     }
}

I just need a way to correct the javascript to allow apostrophes... Any idea..

Many thanks in advance, Paul

3 Answers 3

3
  1. You missed one apostrophe in

    Check Out Rhapsody Tours's Video:

  2. Inside a string you want to escape-escape -> \\\' three backslashes and then apostrophe.

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for your help with this issue... this resolved it!
0

Escape all the quotes and double quotes by putting a backslash in front of them:

text.replace(/'/g, "\\'").replace(/"/g, '\\"')

E.g.

"foo's bar is \"baz\"".replace(/'/g, "\\'").replace(/"/g, '\\"') 

Yields

foo\'s bar is \"baz\"

Edit: Just realised this I assumed you were getting your content dynamically somehow. If you're just shoving a hard-coded string in there, then the answers above re: just escape the spare quote, will suffice.

Comments

-1
document.getElementById('tbDetails').innerHTML = '<textarea name="taComments" style="height:40px;width:333px" class="f1 greytext" onblur="sb_textarea_onblur(this, \'Check Out Rhapsody Tours\'s Video:\')" onfocus="sb_textarea_onfocus(this, \'Check Out Rhapsody Tours\'s Video:\')">Check Out Rhapsody Tours\'s Video:</textarea>';

In this code you can see a backslash before the apostrophe which converts it to a part of the string. The problem was caused by this apostrophe because your Check Out Rhapsody Tours' makes the browser think the string is ended unless you replace ' with \'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.