javascript - Get value from iput HTML / JS -


i have following html code:

<form name="register" action="register.aspx" method="post" runat="server" style="margin-top: 15px;" onsubmit="return validateprofile(formname='register');" >  <p>  name* : <input id="firstname" type="text" name="firstname"/> </p>  <input type="submit"> </form> 

and js code:

function isempty(field) {     return (field == "" || field == null) }  function validateprofile(formname) {     var fname = document.forms[formname]["firstname"].value; return false;     var g = (isempty(field));     alert(g);  } 

the problem in line :

var fname = document.forms[formname]["firstname"].value; return false;

i error message :

uncaught typeerror cannot read property 'firstname' of undefined

if write instead of line above line

document.getelementbyid("firstname").value

it works great, question why document.forms[formname]["firstname"].value not work?

document.forms[formname]["firstname"] try access property on javascript htmlformelement forms object. [id] syntax can used on collection. access element id firstname use document.forms[formname].elements['firstname'].value


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -