1

I'm using a javascript function in my html click and pass three parameters in it

<div class="save_button" onclick="savetext('text','text_2000','Plan to share my experiences this week.')">Submit</div>

This is working fine for the function

function savetext(type,text_id,text_body){ 
    // mycode
}

Note: The 3rd parameter can be any text

When the 3rd parameter came like

<div class="save_button" onclick="savetext('text','text_2001','It's hot today, what I'll do? (better not to go outside!).Let's have an ice cream')">Submit</div>

showing the error in console

Uncaught SyntaxError: missing ) after argument list

I know it due to additional ( , ) and ' coming on the parameter. But I couldn't able to remove that 3rd parameter because its dynamic

How it can fixed.??

4
  • 1
    Look at the syntax highlighting.. Commented Aug 25, 2015 at 5:31
  • 1
    are you sure you mentioned a right example?? you are calling function savetext and defining funtion savecomment Commented Aug 25, 2015 at 5:33
  • @ShowStopper Thanks for showing it.. but its same.. both case issue is generating.. I have edited my question :) Commented Aug 25, 2015 at 5:36
  • 1
    Your third argument needs some handling of escape character as it contains single quote 'It's hot today, what I'll do? (better not to go outside!).Let's have an ice cream' Commented Aug 25, 2015 at 6:35

2 Answers 2

6

You're using quotes ' inside quote. That is breaking the string and resulting in error.

Escape the quote by preceding with \.

See the highlighted changes in below code.

<div class="save_button" onclick="savetext('text','text_2001','It\'s hot today, what I\'ll do? (better not to go outside!).Let\'s have an ice cream')">Submit</div>
<!--                                                             ^^                  ^^                                       ^^ -->
Sign up to request clarification or add additional context in comments.

Comments

4

Just escape any ' in your parameter using \ and I believe you will be fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.