newman Posted November 16, 2006 Share Posted November 16, 2006 hi everybody .i'm wonderin' if there's any way to pass HTML array fields to a JS function as a parameter .suppose i have 3 HTML fields :<input type="text" name="friends[]" value="tom"><input type="text" name="friends[]" value="richard"><input type="text" name="friends[]" value="nick">now i wanna pass these 3 elements to a JS function . i tried MyFunction(friends[]) but i got an error .can any one help me .thank you before hand . Quote Link to comment Share on other sites More sharing options...
fenway Posted November 16, 2006 Share Posted November 16, 2006 I don't know what you mean by "pass an element a function". Quote Link to comment Share on other sites More sharing options...
extrovertive Posted November 17, 2006 Share Posted November 17, 2006 Just do something like this.var obj = document.form.elements;and then loop through obj searching for a type that matches "text"and get the value of it. Quote Link to comment Share on other sites More sharing options...
newman Posted November 17, 2006 Author Share Posted November 17, 2006 sorry , i asked my question badly.i meat the values of elements.like PHP we pass an array to a function :[code]$friends=array('tom','rich','nick');MyFunction($friends);[/code]now i wanna do it in JS .i have these fields values:[code]<input type="text" name="friends[]" value="tom"><input type="text" name="friends[]" value="rich"><input type="text" name="friends[]" value="nick">[/code]how can I pass these values to a JS function as a parameter. Quote Link to comment Share on other sites More sharing options...
fenway Posted November 17, 2006 Share Posted November 17, 2006 As was mentioned earlier, if you iterate though .length of this element, you should be able to get at the values. Quote Link to comment Share on other sites More sharing options...
newman Posted November 18, 2006 Author Share Posted November 18, 2006 thanks .but how can i count this elements . how to know how many fields i have ?in here how many fields named friends[] .thanks again Quote Link to comment Share on other sites More sharing options...
fenway Posted November 19, 2006 Share Posted November 19, 2006 This collection has a length property for this purpose for checkboxes. Quote Link to comment Share on other sites More sharing options...
newman Posted November 20, 2006 Author Share Posted November 20, 2006 god bless you ,i dont know why you dont explain it with an example . i'm not a pro .i tried this : document.MyForm.friends.length but i got an error . Quote Link to comment Share on other sites More sharing options...
fenway Posted November 21, 2006 Share Posted November 21, 2006 Your form field is called "friends[]", not "friends". Quote Link to comment 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.