Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get elements in jQuery starting with a particular pattern and ending with another pattern

Tags:

jquery

<div id="Tab1">
    <input type="text" id="ctl100_Tab1_one_text"/>
    <input type="text" id="ctl100_Tab1_two_text"/>

</div>
<div id="Tab2">
    <input type="text" id="ctl100_Tab2_one_text"/>
    <input type="text" id="ctl100_Tab2_two_text"/>

</div>

I want all textboxes under Tab1 separate to Tab2

like image 227
chugh97 Avatar asked Feb 25 '23 12:02

chugh97


2 Answers

Given the code you posted:

<div id="Tab1">
    <input type="text" id="ctl100_Tab1_one_text"/>
    <input type="text" id="ctl100_Tab1_two_text"/>

</div>
<div id="Tab2">
    <input type="text" id="ctl100_Tab2_one_text"/>
    <input type="text" id="ctl100_Tab2_two_text"/>

</div>

And the question:

I want all textboxes under Tab1 separate to Tab2

$('#tab1 input:text')

However, given the title of your question:

How can I get elements in jQuery starting with a particular pattern and ending with another pattern?

You could use:

$('input:text[id^=startsWithPattern][id$=endsWithPattern]');

Using the ^= (attribute-starts-with selector), and $= (attribute-ends-with selector).

As demonstrated with a JS Fiddle demo.

like image 83
David Thomas Avatar answered May 12 '23 06:05

David Thomas


To get all input types under tab1 use this selector:

$('#Tab1 input');
like image 21
Tjirp Avatar answered May 12 '23 07:05

Tjirp