1

I have the following simple code, however for some reason it trows a Unexpected token "}" error, and I'm not entirely sure why. I have the following code to generate the buttons

var OverlayMessage = ("Do you want to test these settings? <br> <h6>" + protocol + ":" + systemcodeSend + "-" + unitcode + " sending " + state + '</h6> <br> <a id="btnGreen" onclick="checkIfSettingsWork("' + protocol +'", "'+ systemcodeSend +'", "'+ unitcode +'", "'+ state +'");">Yes</a> <a id="btnRed" onclick="toggleVisibility(\'overlayAddDevice\')">No</a>');
        console.log(OverlayMessage);
        overlayMessage(OverlayMessage);

The output of console.log is as followed

<a id="btnGreen" onclick="checkIfSettingsWork("pollin", "31", "14", "on");">Yes</a>

The strange thing is, if I run from the debugger, it works fine without errors. However when being ran trough the onclick event it trows the Unexpected token error.

The function checkIfSettingsWork is not even being ran, I checked this by letting it log to console, and that is returning nothing when being ran by the onclick event however it works when I run the following javascript trough the debugger.

checkIfSettingsWork("pollin", "31", "14", "on");

This is the checkIfSettingsWork function.

function checkIfSettingsWork(protocol, systemcodeSend, unitcode, state) {
    console.log(protocol + systemcodeSend + unitcode + state)

}
1
  • try replacing those double quotes in the parameters with single quotes Commented Dec 29, 2015 at 1:45

4 Answers 4

3

This is invalid html:

 <a id="btnGreen" onclick="checkIfSettingsWork("pollin", "31", "14", "on");">Yes</a>

The " characters are ending your onclick attribute early, so the browser ends up interpreting it as just onclick="checkIfSettingsWork(".

Change it to this for a workaround:

<a id="btnGreen" onclick="checkIfSettingsWork('pollin', '31', '14', 'on')">Yes</a>
Sign up to request clarification or add additional context in comments.

Comments

0

The valid synatax will be

<a id="btnGreen" onclick="checkIfSettingsWork('pollin', '31', '14', 'on')">Yes</a>

1 Comment

Mmh does OP really need a TL;DR answer for barely 6 lines of text?
0

You have many contexts going on here and I think they need to be separated. You are trying to create text, HTML, and JavaScript in this one declaration. Also in you mixed single and double quotes. In your concatenation. You could use both but they have to match.

It's not safe to do all these in one line. Especially the onClick function. I don't want to create a template for the HTML, right out for a JavaScript function, then feed the variables into the function.

You also don't need any quotes for your parameters in your function you could just feed in the variables.

CheckIfSettingsWork( protocol, systemcodeSend, unitcode, state)

Comments

0

You made a low-level mistake.

This is invalid like onclick="checkIfSettingsWork("pollin", "31", "14", "on");"

You need fix " to ' .

Another thing is i think when you declaring OverlayMessage, just my suggestion, maybe you need use " and ' characters by uniform way.

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.