Jump to content

Uncaught SyntaxError: Unexpected token }


Spunky
 Share

Recommended Posts

I am completely bewildered about this error that I am getting. At first I thought it was being caused by the JavaScript that ran with it, but I completely gutted my code and found the perp but I cannot figure out why...

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Untitled Document</title></head><body><div id='genderCheck'><h4>First of all, select your gender:</h4><input type='radio' name='gender' value='male' onclick='userprompt('male','');'>Male<input type='radio' name='gender' value='female' onclick='userprompt('female');'>Female</div><div id='ageCheck'><h4>Now a bit more..</h4><input type='radio' name='age' value='adult' onclick='userprompt('','adult');'>Adult<input type='radio' name='age' value='child' onclick='userprompt('','child');'>Child</div><div id='textPrompt'><h4>Great! Now, go ahead and type a response to your fellow classmates:</h4><textarea id='userResponse'>Type here...</textarea><button type='button' onclick='submitResponse();'>Enter</button></div></body></html>

Uncaught SyntaxError: Unexpected token } test_prompt.html:10

 

This error only occurs when I have parameters in the function calls, without the parameters the functions run fine (except that I need the parameters). It says line 10 when Male or Female is selected and line 12 when Adult or Child is selected.

 

I cannot for the life of me figure out why. I've been staring down this code for forever.

Link to comment
Share on other sites

Dang I was afraid that might be the issue. I'd tried several things to prevent it and ultimately I decided that wasn't the issue. But suppose it is.. The problem is that I ultimately need the code to look like this:

prompt_div = "<div id='genderCheck'><h4>First of all, select your gender:</h4><input type='radio' name='gender' value='male' onClick='userprompt('male','');'>Male<input type='radio' name='gender' value='female' onClick='userprompt('female');'>Female</div><div id='ageCheck'><h4>Now a bit more..</h4><input type='radio' name='age' value='adult' onClick='userprompt('','adult');'>Adult<input type='radio' name='age' value='child' onClick='userprompt('','child');'>Child</div><div id='textPrompt'><h4>Great! Now, go ahead and type a response to your fellow classmates:</h4><textarea id='userResponse'>Type here...</textarea><button type='button' onClick='submitResponse();'>Enter</button></div>";

It all squished together inside a variable to be used in JavaScript to display it when I call it. Can you recommend a better way I can go about this task?

Link to comment
Share on other sites

You can use double-quotes, just be sure to escape them with "

You could break the string into pieces to make it more readable.

var prompt_div = "<div id='genderCheck'>";prompt_div +=     "<h4>First of all, select your gender:</h4>";prompt_div +=     "<input type='radio' name='gender' value='male' onClick='userprompt("male","");'> Male";prompt_div +=     "<input type='radio' name='gender' value='female' onClick='userprompt("female");'> Female";prompt_div += "</div>";prompt_div += "<div id='ageCheck'>";prompt_div +=     "<h4>Now a bit more..</h4>";prompt_div +=     "<input type='radio' name='age' value='adult' onClick='userprompt("","adult");'> Adult";prompt_div +=     "<input type='radio' name='age' value='child' onClick='userprompt("","child");'> Child";prompt_div += "</div>";prompt_div += "<div id='textPrompt'>";prompt_div +=     "<h4>Great! Now, go ahead and type a response to your fellow classmates:</h4>";prompt_div +=     "<textarea id='userResponse'>Type here...</textarea>";prompt_div +=     "<button type='button' onClick='submitResponse();'>Enter</button>";prompt_div += "</div>";

Usually, rather than making Javascript to write all this, it's already on the page and you just use CSS and Javascript to make it appear or disappear.

Link to comment
Share on other sites

Oooh thanks for the tip of how to make it more readable I like that. I actually do have some of that appear and reappear using CSS and JavaScript, but JavaScript initially puts the whole thing there in the first place. But I will consider doing it differently. Thanks so much for this tip!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...