hisoka Posted March 8, 2015 Share Posted March 8, 2015 in this tutorial http://www.w3schools.com/js/js_validation.asp there is this script : function validateForm() { var x = document.forms["myForm"]["fname"].value; if (x == null || x == "") { alert("Name must be filled out"); return false; }} I do not understand this : var x = document.forms["myForm"]["fname"].value; this : document.forms["myForm"]["fname"] is an array so why it is not like this ?? var x = ["my form" , "fname"]; and why it is between two strings (document.forms and value) : document.forms["myForm"]["fname"].value it confuses me . I cannot understand this Link to comment Share on other sites More sharing options...
dsonesuk Posted March 8, 2015 Share Posted March 8, 2015 Its an old method of targetting specific input elementdocument.forms = all forms within page.document.forms["myForm"] = equals target form with name value of 'myForm'document.forms["myForm"]["fname"] = target input with name value of 'fname' within a form with name value of 'myForm'document.forms["myForm"]["fname"].value = get value of input with name value of 'fname' within a form with name value of 'myForm'. Link to comment Share on other sites More sharing options...
hisoka Posted March 9, 2015 Author Share Posted March 9, 2015 ------ ---- - --document (object root)----------------------- | | | | | | | | | | --forms images links cookie domain | | | | | | | | | | www.w3schools.com input(1,2,3) src(1,2,3) href(1,2,3) document is the object root and has a collection of elements that differ by type like (forms , domain , links , cookie , images , forms). These elements are the properties of the object root . They have values like(input , src , href....) . The elements of the root object are organised and ordered and can be accessed by index- like in arrays , elements are ordered and accessed by their index numbers in arrays- for example forms[0] , forms[1] and so on .... so in order to access the forms[0] we need to do like this document.forms[0].(variable that holds the name of the form) for example : document.forms[0].id the document.forms[0] is telling javascript that we are dealing with properties of form type the id make our query more specific and tells javascript that we want the value of the first form in the document or object root. However in this link : http://www.w3schools.com/jsref/coll_doc_forms.asp I accessed the source code and find this form <form role="form"><div class="form-group"><label for="err_email">Your Email:</label><input class="form-control" type="email" id="err_email" name="err_email" /></div><div class="form-group"><label for="err_email">Page address:</label><input class="form-control" type="text" id="err_url" name="err_url" disabled="disabled" /></div><div class="form-group"><label for="err_email">Description:</label><textarea rows="10" class="form-control" id="err_desc" name="err_desc"></textarea></div><div class="form-group"> <button type="button" class="btn btn-default" onclick="sendErr()">Submit</button></div></form><form role="form"><div class="form-group"><label for="err_email">Your Email:</label><input class="form-control" type="email" id="err_email" name="err_email" /></div><div class="form-group"><label for="err_email">Page address:</label><input class="form-control" type="text" id="err_url" name="err_url" disabled="disabled" /></div><div class="form-group"><label for="err_email">Description:</label><textarea rows="10" class="form-control" id="err_desc" name="err_desc"></textarea></div><div class="form-group"> <button type="button" class="btn btn-default" onclick="sendErr()">Submit</button></div></form> I tried to access the form like this alert(document.forms[0].role) but i did not succeed : it says undefined . I tried to change the index number to 2 , 3 , 4 ...7 but I did not work although my command is true . Why it did not work although my command is true? Link to comment Share on other sites More sharing options...
hisoka Posted March 9, 2015 Author Share Posted March 9, 2015 (edited) In order to better understand this : document.forms["myForm"]["fname"] I write this little script <!DOCTYPE html><html><body><form name="console" onsubmit="return show()"><input type="text" name="hisoka" /><p>Enter your name</p><input type="submit" name="mysubmit" value="Click!" /></form><br><form name="log" onsubmit="return show1()"><input type="text" name="admin" /><p>Enter your name</p><input type="submit" name="mysubmit" value="Click!" /></form><b><form name="sys" onsubmit="return show2()"><input type="text" name="gini" /><p>Enter your name</p><input type="submit" name="mysubmit" value="Click!" /></form><br><form name="user" onsubmit="return show3()"><input type="text" name="brock" /><p>Enter your name</p><input type="submit" name="mysubmit" value="Click!" /></form><script>function show(){var a = document.forms["console"];alert(a);}function show1(){var b = document.forms["log"];alert(;}function show2(){var c = document.forms["sys"];alert(c);}function show3(){var d = document.forms["user"];alert(d);}</script></body></html> but it shows for all : object html form element as an output what am I missing ? Edited March 9, 2015 by davej Link to comment Share on other sites More sharing options...
justsomeguy Posted March 9, 2015 Share Posted March 9, 2015 That's exactly what it should show when you try to alert a form element. Link to comment Share on other sites More sharing options...
dsonesuk Posted March 9, 2015 Share Posted March 9, 2015 You have assigned just the form object to a variable with a specific name, you now have tell it what to do, access input using specific name value to access its input value, as shown in my 1st post, as the only thing you can alert from the form is its name valuevar d = document.forms["user"].name; alert(d);ORvar d = document.forms["user"]; alert(d.name); Link to comment Share on other sites More sharing options...
hisoka Posted March 10, 2015 Author Share Posted March 10, 2015 thank you what about this : "I tried to access the form like this alert(document.forms[0].role) but i did not succeed : it says undefined . I tried to change the index number to 2 , 3 , 4 ...7 but I did not work although my command is true . Why it did not work although my command is true?" document.forms["user"].name gives user as output document.forms["console"].name gives console as output I tried the same concerning this http://www.w3schools...l_doc_forms.asp <form role="form"> ..... alert(document.forms["form"].role) but it did not work why? and how it works ? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2015 Share Posted March 10, 2015 Why it did not work although my command is true?If the browser says that it is undefined then it's undefined, it's not "true". The browser isn't going to make a mistake like that, you're the one making the mistake. You can use console.log(document.forms) to look at the entire forms collection and see everything in it if you want to see what's there.alert(document.forms["form"].role) but it did not work why? and how it works ?Form elements do not have a "role" attribute, so the form object is not going to have a role property.https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form Link to comment Share on other sites More sharing options...
dsonesuk Posted March 10, 2015 Share Posted March 10, 2015 But you can create the custom attribute and access its valuevar x = document.forms["form"];x.role = "whatever";alert(x.role) Link to comment Share on other sites More sharing options...
hisoka Posted March 11, 2015 Author Share Posted March 11, 2015 "Form elements do not have a "role" attribute, so the form object is not going to have a role property." But here : <form role="form"> The attribute of the form is "role" . Is not it ? so Why this : alert(document.forms["form"].role) did not work ? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2015 Share Posted March 11, 2015 Look at the page I linked to. Under the "Attributes" section, do you see "role" there? Link to comment Share on other sites More sharing options...
hisoka Posted March 11, 2015 Author Share Posted March 11, 2015 either I am wrong or you misunderstood me here how it is : in <form class="gsc-search-box gsc-search-box-tools" accept-charset="utf-8"> class is the attribute of form . in <form id="myform"> id is the attribute of form in <form name = "logoutform"> name is the attribute of form right? only specific words can be used as form attributes . the word "role" cannot used as an attribute to a form However in this form( which I found in the source code of this page http://www.w3schools..._validation.asp) <form role="form"><div class="form-group"><label for="err_email">Your Email:</label><input class="form-control" type="email" id="err_email" name="err_email" /></div><div class="form-group"><label for="err_email">Page address:</label><input class="form-control" type="text" id="err_url" name="err_url" disabled="disabled" /></div><div class="form-group"><label for="err_email">Description:</label><textarea rows="10" class="form-control" id="err_desc" name="err_desc"></textarea></div><div class="form-group"> <button type="button" class="btn btn-default" onclick="sendErr()">Submit</button></div></form> the attribute of the form is role . So is not this considered wrong as role cannot be an attribute of a form ??? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2015 Share Posted March 11, 2015 only specific words can be used as form attributes . the word "role" cannot used as an attribute to a formRight, "role" is not a predefined form attribute which is why the form object in Javascript does not have a property called "role".HTML5 is more relaxed than previous versions of HTML, I believe you can use custom attributes and still have the document validate (I know you can use custom data- attributes, I'm not sure about others). The role attribute specifically is part of ARIA, there's a little bit about it here:http://html5forwebdesigners.com/today/https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIAYou can research more about ARIA if you want to. 1 Link to comment Share on other sites More sharing options...
hisoka Posted March 11, 2015 Author Share Posted March 11, 2015 it was implemented in html5 . It is something new Link to comment Share on other sites More sharing options...
hisoka Posted March 13, 2015 Author Share Posted March 13, 2015 In this script <!DOCTYPE html><html><body><form name="console" onsubmit="return show()"><input type="text" value="hisoka" /><p>Enter your name</p><input type="submit" name="mysubmit" value="Click!" /></form><script>function show(){var a = document.forms["console"]["hisoka"].value;alert(a);}</script></body></html> the value hisoka is shown in the form and this is normal but I did not succeed in making the alert box show it . Why the value of hisoka could not appear in the alert box ?? Link to comment Share on other sites More sharing options...
thescientist Posted March 13, 2015 Share Posted March 13, 2015 How are you testing? Checking your developer tools? Or just guessing? You need to debug and troubleshoot. I would revaluate how you are trying to obtain that value. I would advise against your approach, and would advocate you use the DOM API using a method like getElementById Link to comment Share on other sites More sharing options...
dsonesuk Posted March 13, 2015 Share Posted March 13, 2015 You don't have a form input element named 'hisoka' to retrieve the value from, just input with no name attribute with only a value of 'hisoka'. 1 Link to comment Share on other sites More sharing options...
hisoka Posted March 13, 2015 Author Share Posted March 13, 2015 (edited) thank you dsonesuk: it becomes like this : <!DOCTYPE html><html><body><form name="console" onsubmit="return show()"><input name ="moderator" type="text" value="" /><p>Enter your name</p><input type="submit" name="mysubmit" value="Click!" /></form><script>function show(){var a = document.console.moderator.value;alert(a);}</script></body></html> and this <!DOCTYPE html><html><body><form name="console"><input name = "moderator" type ="button" type="text" value="7" onclick="return show()"/></form><script>function show(){var a = document.console.moderator.value;alert(a);}</script></body></html> Edited March 13, 2015 by hisoka Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now