-1

There is dynamically build Html block with some elements having attribute input-readonly-container. I need to select elements which are not in sub input-readonly-container.

<div id='root' input-readonly-container>
    <input id='1' type="text" />
    <input id='2' type="text" />
    <input id='3' type="button" value="Btn"/>
    <div id="child1" input-readonly-container>
        <input id='5' type="text"/>
        <input id='6' type="text"/>
    </div>
    <div id="child2">
        <input id='7' type="text"/>
        <input id='8' type="text"/>
    </div>

</div>

In this sample I need to select only elements with Id 1,2,3,7,8. Child container with [input-readonly-container] and internal elements (5,6) should be skipped.

Selection should happen inside of context of root element.

var root=$('#root');
$('selectorHere', root); 

and this should select only id=1,2,3,7,8.

If child doesn't have [input-readonly-container], selection should pick all children elements (7,8)

If I run same selector with root=$('#child'), it should select id=5,6 only

So idea of selector to select elements of container with [input-readonly-container] which are not children of another element with attribute [input-readonly-container]

Vitaliy Markitanov
  • 2,205
  • 1
  • 24
  • 23

4 Answers4

2

Try this:

jQuery('div[input-readonly-container]:first').children().not('[input-readonly-container]')
jedifans
  • 2,287
  • 1
  • 13
  • 9
1

$("div[input-readonly-container]:first > input")

You can use descendant selector to avoid grand child elements.

:first to select document's first element div containing attribute input-readonly-only

> input to select only descendants within div:first.

ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
0

Add a class to the elements you want to select with jQuery and use a single selector in your Script.

<div input-readonly-container>
<input id='1' type="text" class="jquery-selector"/>
<input id='2' type="text" class="jquery-selector"/>
<input id='3' type="button" value="Btn" class="jquery-selector"/>
    <div id="child">
        <input id='5' type="text"/>
        <input id='6' type="text"/>
    </div>
</div>

jQuery code

var listItems = jQuery('.jquery-selector');

Also its not good practice to start id's with numbers.

Try to keep code as simple and easy to read as possible.

0

Filter the childrens of the div, See the js fiddle here, https://jsfiddle.net/rahulpandey034/xot37hrd/

$(document).ready(function(){
    $("div[input-readonly-container]:first > input").css({"color": "red", "border": "2px solid red"});
});
Rahul Pandey
  • 435
  • 1
  • 3
  • 13