Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  6] [ 6]  / answers: 1 / hits: 77065  / 13 Years ago, sat, august 6, 2011, 12:00:00

I have a Javascript function which should update a hidden input field in my form with a number that increments every time the function is called.



It worked originally with getElementById() however because I had to redesign my form I cannot use the php function to assign an individual ID to the element so all I have is a unique name for that element.



So instead I decided to use getElementsByName() from Javascript to modify the element.



Here is the HTML of that element



  <input type=hidden value= name=staff_counter>


This is my Javascript code:



window.onload=function()
{

//function is activated by a form button

var staffbox = document.getElementsByName('staff_counter');
staffbox.value = s;


s++;
}


I am getting no errors on Firebug when the function is called and the input field is not getting a value given to it.



It was working with getElementById() but why all of a sudden it does not work with getElementsByName()?




  • -I have checked that it is the only unique element in the document.

  • -I checked for any errors on Firebug when activating the function



Here is the code I use from Codeigniter to make the element



// staff_counter is name and the set_value function sets the value from what is
//posted so if the validation fails and the page is reloaded the form element does
// not lose its value

echo form_hidden('staff_counter', set_value('staff_counter'));


Thanks


More From » html

 Answers
171

document.getElementsByName() returns a NodeList, so you have to access it by an index: document.getElementsByName('staff_counter')[0] (depending on how many of these you have).



You also have access to a length property to check how many Elements were matched.


[#90777] Friday, August 5, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payne

Total Points: 527
Total Questions: 108
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;