Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use jQuery find() for more than one element type at the same time?

Tags:

jquery

I am using the following function to change the padding in a column on my forms:

function padTitles() {     $('#option-grid #dataTable tr, #topic-grid #dataTable tr')         .each(function () {             var tds = $(this).find('input'),         text = tds.filter('[id^="input_TempRowKey_"]').val(),         tdToPad = tds.filter('[id^="input_Title_"]'),         pad;             if (/\.0$/.test(text)) {                 pad = 10;                 level = 1;             } else {                 pad = 35;                 level = 2;             }             tdToPad.css('margin-left', pad);             a = tdToPad.closest('tr');             if (tdToPad.closest('tr').get().className) {                 tdToPad.closest('tr').get().className = tdToPad.closest('tr').get().className.replace(/\blevel\-.*?\b/g, 'level-' + level);             } else {                 tdToPad.closest('tr').addClass('level-' + level)             }         }); } 

It works well for this form HTML:

<td id="title_1" class=" ">    <input type="text" value="Tests" name="item.Title" id="input_Title_1" > </td> 

Now I would also like it to work for the following HTML:

<td id="title_1" class=" ">       <textarea name="item.Title" id="input_Title_1">Tests</textarea> </td> 

Is there a way I can change this function so it works for either an input or textarea? I assume the way to do this is to change var tds = $(this).find('input'), however I am not sure how to change it or even if it's possible to change to "find" either a textarea or an input.

like image 419
Alan2 Avatar asked Jul 28 '12 05:07

Alan2


People also ask

How can use two find in jQuery?

Syntax. To select multiple elements of an html page using multiple elements selector, we pass the element names inside parenthesis, in double quotes, separated by commas. For example: $(“div, p, h2”) this will select all the div, p and h2 elements of a page.

Can you select multiple elements in jQuery?

In jQuery, you can select multiple elements by separate it with a comma “,” symbol.

How does find work in jQuery?

jQuery find() MethodThe find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.

How do you select two elements with the same class?

To select multiple elements with same class you can use attribute selector like $('[id=container]') but it is better to use class and keep the ids of elements unique.


2 Answers

Use a comma to union multiple queries:

var tds = $(this).find('input, textarea'); 

You can also use :input as a selector, but it's not as efficient and may also include some things you'd rather not include.

like image 186
Ry- Avatar answered Oct 18 '22 09:10

Ry-


You can use .children() to select the tds child whatever it is

var tds = $(this).children(); 
like image 33
Musa Avatar answered Oct 18 '22 09:10

Musa