ecco88
ecco88

Reputation: 636

How to access elements from a certain form and certain type with jquery

so I have a page with multiple forms and I would like to be able to access input elements of type="datetime" within a particular form. I know I can do $('input[type="datetime"]') but that would give me all input tags on the page. I also cannot use the "form" attribute because not all browser use it (Sigh IE). Worse scenario for me is to do something like:

$(document.forms["..."].elements).each(function() {
    if (this.type="datetime") {.....}
});

but I am sure this can be done in jQuery with one selector. Can someone tell me how do this with one selector?

Upvotes: 2

Views: 96

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

There are multiple ways to do it

Solution 1.
use descendant selector
ex:

$('#yourform input[type="datetime"]') //or
$('.yourform input[type="datetime"]') //or
$('form:eq(3) input[type="datetime"]')


Solution 2: Use context based look up
Ex:

$('input[type="datetime"]', yourform)

Upvotes: 0

Brandon
Brandon

Reputation: 39182

Without seeing some HTML this is just a shot in the dark. But if you give your forms an id you can do:

$("#yourFormId input[type='datetime']");

If you do not have ids, but you know the number, then this might do it:

$("form:eq(4) input[type='datetime']");

Upvotes: 2

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

Add id to your form and then select DOM inside of that form as below.

$('#form input[type="datetime"]')

Upvotes: 3

Related Questions