input value into js variable

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
iam4423
New php-forum User
New php-forum User
Posts: 50
Joined: Fri Jan 20, 2012 8:00 am

Thu Jul 19, 2012 9:05 am

Hi, im trying to put the value of a hidden form field into a JavaScript variable but when i try to display the variable i just get NaN. Heres what ive got:

Code: Select all

function addElement(){
var count = document.getElementById("count").value;
     ++count;
          var AdEl = document.createElement('div');
          AdEl.innerHTML = "<input type='text' name='ident" + count + "' value='"+count+"' /><br /><textarea name='style" + count + "' cols='60' rows='5'></textarea><hr>";
          document.getElementById('adds').appendChild(AdEl);
		  document.getElementById("count").value = count;
}
and my form field is

Code: Select all

<input type='hidden' name='count' value='7' id='count'>
if i set the variable manually it works fine but it just wont get the value out of the form field.

any help would be appreciated, cheers!

**** UPDATE ****
if i return the value of count right after i grab it from the hidden field i get 'undefined'

iam4423
New php-forum User
New php-forum User
Posts: 50
Joined: Fri Jan 20, 2012 8:00 am

Thu Jul 19, 2012 9:49 am

Okay, im an idiot the issue was that i was defining the variable outside of the function so it was grabbing the value on page load not when i ran the function

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests