Set value to unique name via getElementsByName without ID?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Set value to unique name via getElementsByName without ID?

Post 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';
prashantAG
Forum Newbie
Posts: 7
Joined: Wed Apr 30, 2008 1:31 am

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

Post 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.
Post Reply