-1

I have a script as follows where element is a number:

function updateList(element) {
    var out_list = "<Select name='item" + element + "' onchange=getQualType(this.value, 'qualType" + element + "')>";
.... add options here ...
    out_list += "</Select>"
    document.getElementById('dropdown_list' + element).innerHTML = out_list;
}

However what is in the browser is:

<select name="item40" onchange="getQualType(this.value," 'qualtype40')="">
</script>

Can anyone point out what is causing the incorrect quotes in the output?

UPDATE

Spotted it:

    var out_list = "<Select name='item" + element + "' onchange=getQualType(this.value, 'qualType" + element + "')'>";

Single quote after bracket - thanks all.

5
  • This should work just fine. Commented Aug 30, 2016 at 11:43
  • The JS isn't "wrong" as to the select it's output, per se. Its writing out your select element literally (and thus, correctly). Your string that creates it is technically the issue. You could just create a handler for the select through JS, and handle the onchange event that way, instead :) Commented Aug 30, 2016 at 11:44
  • no error in this...its fine Commented Aug 30, 2016 at 11:44
  • Just spotted it - missed quote after the bracket!! Commented Aug 30, 2016 at 11:44
  • 2
    you have to put quotes around the onchange-script. this way, it can't contains any spaces, like the one before 'qualType" + element + "' Commented Aug 30, 2016 at 11:45

2 Answers 2

1

You can try this, you had problems at escaping quotes:

var out_list = '<select name="item' + element + '" onchange="getQualType(this.value, \'qualType' + element + '\')">';
Sign up to request clarification or add additional context in comments.

Comments

0

I would start by quoting the onChange. If you don't do it, the browser makes its own decisions:

 var out_list = "<Select name='item" + element + "' onchange=\"getQualType(this.value, 'qualType" + element + "');\">";

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.