views:

69

answers:

5

Is there any such function to get all form tags?

by form tags I mean all <input>, <select> etc?

Thanks

+1  A: 
var elements = getElementsByTagName("input")
    .splice(getElementsByTagName("select"));

// splice to your hearts content
Justin Niessner
+3  A: 

You could use jquery:

$(':input')

Otherwise, given the id of the form, all of its fields are given by

var formid = "foo";
var myform = document.getElementById( formid )
if (myform != null) {
    // myform.elements is an array of the fields
}

And if you just wanted to find all the select elements in the page, use getElementsByTagName()

var all_selects = document.getElementsByTagName('select')
Lachlan Roche
+2  A: 

Using jQuery the following will make them pink:

$("input, select, textarea").css({background: "pink"});
svinto
The OP didn't mention jQuery. Had he, @Lachlan's response is better: http://stackoverflow.com/questions/2290991/get-all-form-tags-with-javascript-function/2291023#2291023
Jonathan Sampson
A: 

A framework is overkill for this:

document.getElementsByTagName("input")
document.getElementsByTagName("select")
zincorp
A: 
function getFormElements() {
  var ary = [];
  // use the full list of supported form elements below if this is not exhaustive
  var elementNames = ['input','select','textarea','button']; 
  for (var i=0; i < elementNames.length; i++) {
    ary.concat(document.getElementsByTagName(elementNames[i]));
  }
  return ary;
}
Robusto