Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a form value w/a Space in it's Name 1

Status
Not open for further replies.

glwong

Programmer
Jul 13, 2001
15
US
I am trying to reset the value
"Online HelpDate"
when a check box is clicked,
but because it has a space in it's
name it doesn't work.


Code:
<input class=smallFont checked=checked type=checkbox name="Online Help" value="Online Help" onclick="document.mainForm.Online HelpDate.value=''">


I've tried accessing it via "document.mainForm.Online&nbsp;HelpDate"
but it doesn't work, any suggestions?

Thanks in advanced,
Greg
 
Here's a working example of how to deal with elements with spaces in the name, and forms with spaces in the name (should you ever need that):
Code:
<script language=JavaScript>
function displayText() {
   var str = document.forms["blah Form"].elements["blah Text"].value;
   alert(str);
}
</script>
<body>
<form name='blah Form'>
<input type=text name='blah Text'><br>
<input type=button value='Click Me' onclick='displayText()'>
</form>
</body>

-kaht

banghead.gif
 

You should not be using NAME attributes with a space.

From W3C:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Hope this clarifies things for you,
Dan
 
thanks kaht, i ulitmately used similiar code
with an eval statement, which was a little tricky..
here's a snippet of the code, with hard coded values
(I used dynamic vars)
Code:
function clearIt(name)
{
var str ="\""+name+"\"";
eval("document.forms.mainForm.elements["+str+"].value='';");
}

<form name="mainForm">
<input type=checkbox name="has space"  onclick="clearIt('has space')"></input>
 
along with what Dan mentioned, why not just make a practice of using "_" as the separator rather than a space ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top