1

In the below table which is generated dynamically, I want to traverse and find html controls (input,select,textarea) using JavaScript DOM. I don't want span and div values.

Can anyone give an idea on how to traverse?

 <html>
     <head>Html</head>
         <body>
            <tr>
                  <td>
                    <div id="one"></div>
                  </td>
                   <td>
                      <input type="text" name="textbox" id="txtid" value=10"/>
                   </td>
                   <td>
                      <select id="cuskstatus"  name="cuskstatus" class="selectStyleBorder">
                                <option selected value="LOCAL">Local</option>
                                <option value="GLOBAL">Global</option>
                       </select>
                  </td>
              </tr>
              <tr>
                    <td>
                        <textarea id="tareaid" name="" value="10"/>
                    </td>
                    <td>
                        <span id="span1">value</span>
                    </td>
              </tr>
           </body>
    </html>
3
  • If the items order is not important you can use document.getElementByTagName. Commented May 16, 2011 at 18:05
  • Can you use jQuery or are you limited to plain JavaScript ? Commented May 16, 2011 at 18:08
  • @jnpcl javascript much better but jquery ok no problem Commented May 16, 2011 at 18:22

3 Answers 3

4

I would do it like this

var desired_tag_names = new Array('select', 'input', 'textarea');

function getElements(e) {
   var elements = new Array();
   for (i=0; i<e.length; i++) {
      var el = document.getElementsByTagName(e[i]);
      for (j=0; j<el.length; j++) {
         elements.push(el[j]);
      }
   }
   return elements;
}

var all_the_desired_elements = getElements(desired_tag_names);
Sign up to request clarification or add additional context in comments.

4 Comments

@teneff thanks but requirement is if we take a row we must get all html controls and then only switch over to next row...let me know if u have any doubts...
@teneff please explain these getElements(document.getElementById('row199'), desired_tag_names); i am new to js...
@teneff one more thing table is generated dynamically so i dont know the id document.getElementById('row199') as you mentioned row199
the first parameter passed to the function is a refference of the element with id="row199"
1

In plain JavaScript, just use form.elements.

Assuming that you've a <form id="formid">:

var inputs = document.getElementById("formid").elements;
// ...

Or if you want to traverse all forms of the document on a per-form basis, just use document.forms to get all forms first.

for (var i = 0; i < document.forms.length; i++) {
    var form = document.forms[i];
    var inputs = form.elements;
    // ...
}

If you're already using jQuery, or are open to, use :input selector.

var $inputs = $(':input');
// ...

Comments

0

Applying jQuery makes this problem trivial:

$('input','select','textarea')

This will return a jQuery collection of all input, select and textarea elements.

6 Comments

It's not a good solution to include whole library just to collect few elements on the page :)
Good is subjective. From a pragmatic perspective of getting things done quickly with the right tools, it's a great solution.
Using jQuery for this is great if you already use jQuery in your project. Including jQuery just to do this is like hunting rabbits with nuclear weapons.
I wasn't going to down-vote this, but your comment made me do it, sorry. Suggesting that jQuery is the "right tool" to do a simple DOM-selection because the developer can't be arsed to do his job correctly is simply not ok in my book. You do realise that the end user will suffer a performance penalty for that lazy decision on every single page load, right?
@Adrian: Not too much of a penalty, if they already have jQuery cached from the CDN. :)
|

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.