mongotop mongotop - 23 days ago 7
Javascript Question

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

This is the code I'm using:

<!DOCTYPE html>
<html>
<head>
</head>

<header>
<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;
}
}
//-->
</script>

</header>

<body>

<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)">
</form>

</form>

</section>

</body>


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.

Thanks!

Answer

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:

document.first['CHARLES-STEVEN'].value