#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0

    Javascript and HTML input element naming conventions


    Can I use numeric characters as a name of an input element in a form, and also use that element in Javascript? For example:

    <input type="radio" name="0" value="val1"/>Radio 1<br>
    <input type="radio" name="0" value="val2"/>Radio 2<br>

    Now, how can I reference each radio button? Here's part of the function I've been trying to get to work.

    for(i=0; i<myForm.numberOfGames.value; i++) {
    var rad = myForm.elements[i]
    if(rad.type == "radio") {
    if(!rad[0].checked && !rad[1].checked) {
    alert("You Some alert message")
    return false;
    }
    }
    }
    return true;


    Should this work? I'm getting the following message:

    "0.checked" is null or not an Object

    How can I make this work? Thanks in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    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 (".").
    (http://www.w3.org/TR/html401/types.html#type-name)

    And then don't use any of JavaScript's reserved words:
    http://devedge.netscape.com/library/...s.html#1004016


    Hope this helps,
    Jeroen
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    OK, I'll change the names. But what about my second question? If I change the name of the input elements to be valid, should my Javascript work? Right now, I still get the same error. I've cleared temp files and cookies too.
    Last edited by mwstein; August 18th, 2003 at 07:03 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    what about my second question?
    Sorry, I'm a bit lazy at this hour... Here's some code that assumes your radio button group has a name 'radio0':
    Code:
    function checkradio(radio) {
    	for (var i=0; i < radio.length; i++) {
    		if (radio[i].checked) return true
    	}
    	return false
    }
    function validate(myForm) {
    	if (checkradio(myForm.radio0) == false) {
    		alert ("some alert")
    		return false
    	}
    	return true
    }
    
    <form name="myform" onsubmit="validate(this)">
    Hope this helps,
    Jeroen

IMN logo majestic logo threadwatch logo seochat tools logo