mongotop mongotop - 1 year ago 65
Javascript Question

Skip a special character '-' on a Javascript function call

This is the code I'm using:

<!DOCTYPE html>

<script language="JavaScript" type="text/javascript">
function pass(InputValue){

if(document.getElementById('storevalues').value == false ) {document.getElementById('storevalues').innerHTML = InputValue;}

else {
document.getElementById('storevalues').innerHTML += " ,"+InputValue;



<form name="first">
<textarea rows="4" cols="50" id="storevalues"></textarea>

<input type="checkbox" id="DAVIDHU" value="DAVIDHU" onclick="pass(document.first.DAVIDHU.value)">

<input type="checkbox" id="CHARLES-STEVEN" value="CHARLES-STEVEN" onclick="pass(document.first.CHARLES-STEVEN.value)">




When you check the first checkbox the value of that checkbox goes to the textarea this case is DAVIDHU,

but when you check the second checkbox, this case is CHARLES-STEVEN the value doesn't go to the textarea, I believe it has something to go with the special character '-' I tried escap using back slash exp : CHARLES-STEVEN but it didn't work, the thing is that I have so many user with a combined first and last name with "-"

any help or suggestion will be highly appreciated.


Answer Source

You can only use dot notation with valid identifiers. CHARLES-STEVEN is not a valid identifier because – you guessed it – of the dash. So switch to bracket notation:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download