Click to See Complete Forum and Search --> : Referencing a element name with a ":" in the name


rstaticn
07-25-2003, 12:56 PM
I'm having troubles here with a piece of js code.


for example if you had some sort of setup like this


<html>
<script language = "javascript">

function checkForm(f){
alert (f.address:street.value);
return false;
}

</script>

<body>

<form onSubmit="return checkForm(this)">

<input type = "textBox" name = "address:street">
<input type = "submit" value = "Click Me">

</form>


</body>

</html>

this results in an error due to the, :

do you know if theres anyway of getting around this?

thanks in advance!

David Harrison
07-25-2003, 02:14 PM
Sure, don't use :. Instead use an underscore, _.

rstaticn
07-25-2003, 02:54 PM
well I would but it I can't due to the fact that the code was already weritten by someone else and many processes depend on the : to be there. So I can't take it out...but nice try,...

pyro
07-25-2003, 03:20 PM
Reference it with the form elements array, like this:

alert (f.elements[0].value);

where 0 is the index of the element in the array.

Or, give it an id and reference it by that, like this:

alert (document.getElementById("theid").value);

rstaticn
07-25-2003, 03:38 PM
hey thanks pyro, using id is good way,


I'm probably going to go that route.


do you know if there's a way to define it using special character preceding it?

kind of like if you want to embed a quotation in a string you'd use \"

or if you want to embed an amperstand you used &amp;

is there a way in js to do this with the colon?


thanks

pyro
07-25-2003, 05:02 PM
Actually, this will work fine for you -- didn't think of it originally.

alert (f.elements["address:street"].value);

rstaticn
07-28-2003, 10:37 AM
nice!

thank you again Pyro, your awsome!

pyro
07-28-2003, 11:10 AM
You bet, man... :)