mostafatalebi Posted January 2, 2013 Share Posted January 2, 2013 Hello everybody How to select All elements of a form and assign them a random value? For instance I have a form which has ten inputs and I want to add to all of them a random value? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/ Share on other sites More sharing options...
requinix Posted January 3, 2013 Share Posted January 3, 2013 Javascript framework? What types of elements? All the same random value or different values for each? Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402877 Share on other sites More sharing options...
codefossa Posted January 3, 2013 Share Posted January 3, 2013 #myform input ? Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402881 Share on other sites More sharing options...
mostafatalebi Posted January 3, 2013 Author Share Posted January 3, 2013 I have put: document.Myform.elements.value = "something"; //but it does not work Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402963 Share on other sites More sharing options...
cpd Posted January 3, 2013 Share Posted January 3, 2013 (edited) Assuming the name of your form is "Myform" the above code should technically work. Can you post your entire form and the Javascript your attempting to use. Edit: Just thought you can try document.forms["MyForm"].elements... Edited January 3, 2013 by CPD Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402965 Share on other sites More sharing options...
mostafatalebi Posted January 3, 2013 Author Share Posted January 3, 2013 This is my code <body> <form action="" method="post" name="exm"> <input type="text" name="name"> <input type="text" name="x"> <input type="text" name="y"> <input type="text" name="z"> <input type="submit" name="submit" /> <script type='text/javascript' > function formValue () { document.forms['exm'].elements.value= "AAA"; } formValue; </script> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402968 Share on other sites More sharing options...
cpd Posted January 3, 2013 Share Posted January 3, 2013 "elements" is an array not an object. You need to cycle through it and store each value in an array then return the array. The "formValue;" just before the closing script tag is not required. Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402972 Share on other sites More sharing options...
mostafatalebi Posted January 3, 2013 Author Share Posted January 3, 2013 I got it and didn't get it I need more instructions....I'm more than too beginner Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402983 Share on other sites More sharing options...
cpd Posted January 3, 2013 Share Posted January 3, 2013 Your current method treats the elements property of the form object as an object with a property of value form = { element: { value: "Somethnig" } }. When in fact its along the lines of form = { elements : array( 0 => Button, 1 => Text, 2 => Checkbox ) } There are further technicalities that going into detail will serve no purpose as you wont understand. So you need to cycle through the array: for(i = 0; i <= document.forms["Myform"].elements.length; i++) { // Code omitted } Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1402993 Share on other sites More sharing options...
codefossa Posted January 3, 2013 Share Posted January 3, 2013 (edited) So you need to cycle through the array: for(i = 0; i <= document.forms["Myform"].elements.length; i++) { // Code omitted } Wouldn't that calculate the form length each time? I think it might be a little better to do something like .. for (var i = 0, n = document.forms["Myform"].elements.length; i <= n; i++) Edited January 3, 2013 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1403046 Share on other sites More sharing options...
cpd Posted January 3, 2013 Share Posted January 3, 2013 .length is of type Number. I'm not sure which method is called by default when using .length but either way the affect will be negligible so either method would suffice. That said I would define n outside the loop for clarity and readability purposes. Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1403064 Share on other sites More sharing options...
mostafatalebi Posted January 4, 2013 Author Share Posted January 4, 2013 I'll come back with test. Quote Link to comment https://forums.phpfreaks.com/topic/272626-how-to-select-all-elements-of-a-form/#findComment-1403183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.