Page 1 of 1

Set value to unique name via getElementsByName without ID?

Posted: Tue May 06, 2008 6:15 am
by JAB Creations
I have about a dozen inputs that I will use JavaScript to set the values to via other scripts. Each input has a unique name so I'd like to set the values via getElementsByName without having to add the exact same value for the id attribute as this just seems excessive. However this doesn't seem to work?

Code: Select all

document.getElementsByName('ce02').value='004';

Re: Set value to unique name via getElementsByName without ID?

Posted: Tue May 06, 2008 7:12 am
by prashantAG
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


see below code it may help for u

Code: Select all

 
<html>
<head>
<script type="text/javascript">
function getElements()
{
var x=document.getElementsByName("myInput");
alert(x.length);
}
</script>
</head>
 
<body>
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<br />
<input type="button" onclick="getElements()" value="How many elements named 'myInput'?" />
</body>
 
</html>
 
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.