0

I have a HTML form where i dynamically create elements and set its name , value attributes .

when i tried to access the value say document .formname.nameoftheelement.value then i get the error that value is undefined.

Then i tried to use the following function to access the values .it returns the input elements as 4 but value as null when i it already has predefined value .

function returnTheStoredValues(getTableName) {
    //Array arrList = new Array(20);
    var tableName = document.getElementById (getTableName);
    console.log("The table name" + tableName);
      if (tableName) {
          var inputs = tableName.getElementsByTagName ('td');
          console.log("the inputs are " + inputs.length);
          if (inputs) {
              console.log("inputs not equal to null")
              for (var i = 0; i < inputs.length; ++i) {

                    console.log("the value in phones table are " + inputs[i].value);
                    //arrList[i] = inputs[i].value;

              }
          }
      }
      //return arrList;
} 

The html code is

Phone
         <table id="email_table">
         <tr>
         <td><h3>Email</h3></td>
         <td><input value="+" type="submit" onClick="checkTheEmailButtonClicked()"></td>
         </tr>
         </table>
        <table>
             <tbody>
                 <tr>
                     <td><input type="submit" value ="Save" onclick="getData();"/></td>
                     <td><input type="submit" value = "Cancel"/></td>
                  </tr>
            </tbody>
        </table>    

Appreciate all your help .

3 Answers 3

1

You seem to want the values of the input elements, so:

function returnTheStoredValues(getTableName) {
    var arrList = [];
    var table = document.getElementById(getTableName);
    var inputs = table.getElementsByTagName('input');

    for (var i=0, iLen=inputs.length; i<iLen; i++) {
      arrList[i] = inputs[i].value;
    }
    return arrList;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Because you're getting the TD's and not the INPUT's?

var inputs = tableName.getElementsByTagName('td');

Should be

var inputs = tableName.getElementsByTagName('input');

By the way, if you use a Javascript framework, your code will be happier.

4 Comments

How will a framework fix an error like using the wrong tagName?
@RobG - It won't. jQuery isn't THAT magical =) But in the long run, wouldn't you say it's worth it to learn?
@anon - it depends. If I was a newbie that needed to get something working in a real hurry and had no idea what I was doing, using a framework likely makes sense. But if efficient, cross-browser code is important in a non-trivial program, frameworks are generally unsuitable. And using one means you likely won't learn how either.
@anon - PS. It can be seen from my answer that the code that does the work is no more than the offered jQuery solution, setting up the variables takes a couple of lines. I know which one will run faster and in pretty much any browser since IE 5 (and adding support for it would be very easy, but I suspect there aren't too many people still using it on the web). jQuery can't do that.
0

You really need to look into using jQuery for accessing elements through JavaScript.

You could then re-write your function to the following:

function returnTheStoredValues(getTableName) {
    return $("#email_table input").map(function() {
        return $(this).val();
    }).get();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.