Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2012
    Location
    Canada
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    get all form values by name

    Sorry to bother again but I've been googling for hours and can't find what I'm looking for.
    I have a form with several input type=text grouped in to categories by same name.
    Code:
    <input type="text" name="illnesses" value=""><br/>
    <input type="text" name="illnesses" value=""><br/>
    <input type="text" name="illnesses" value=""><br/>
    <input type="text" name="illnesses" value=""><br/>
    <input type="text" name="illnesses" value=""><br/>
    I want to get the values using getElementsByName and write them into a div.
    This isn't working.

    Code:
    var illnesses=document.getElementsByName("illnesses").value;
    
    document.getElementById("person1").innerHTML=illnesses

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Code:
    var illnessesList =document.getElementsByName("illnesses");
    var illnessesArray = [];
    for (var i = 0; i < illnessesList.length; i++) illnessesArray[i] = illnessesList[i].value;
    document.getElementById("person1").innerHTML=illnessesArray.join(',');
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Nov 2012
    Location
    Canada
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was thinking I could avoid a "for" statement because the result was a text value. Doh! So I need a "for" statement because the same names creates an array-in-waiting, so to speak.
    I came across "Join" in my googling but only once. This should be interesting to implement, can't wait! Thanks for being so willing to help me. I appreciate the help and the understanding I'm gaining....feeling great!

  • #4
    New Coder
    Join Date
    Nov 2012
    Location
    Canada
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes! It writes!
    One downside:
    Upon implementing, I found that the script writes commas if the user doesn't use all the fields.
    I guess I need an "if" statement...
    if input has value, then write.
    ...off to google "how to detect if an input array has value".

    I found a code that gets the values by form and name and modified it to this:

    Code:
    var illarray=document.getElementById("allmediform").illnesses;
    
    var illnessesstring="";
    var e;
    
    for (e=0; e<illarray.length; e++)
    	{
    	if (illarray[e].value)
    	{
    	illnessesstring= illnessesstring + illarray[e].value + ", ";
    	}
    	}
    
    document.getElementById("person1").innerHTML=illnessesstring;
    Last edited by jswannabe; 08-05-2013 at 04:23 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •