MojtabaSh
MojtabaSh

Reputation: 637

how can select all element except of a div and child of divs in jquery

I want select all HTML elements except of a div and child of div for assign an event

the elements is like this:

<span 1 id="select1"></span>
<span 2 id="select2"></span>
<span 3 id="select3"></span>
<span 4 id="select4"></span>
<span 5 id="select5"></span>
<span 6 id="select6"></span>

<!-- here is elements that i don't want select that -->
<div id="dontSelect">
<table>
    <tr>
      <td>
        don't select my element
      </td>
     </tr>
</table>
</div>
<!-- here is elements that i don't want select that -->

how can select like this?Thanks

Upvotes: 0

Views: 266

Answers (2)

Rohan Kumar
Rohan Kumar

Reputation: 40639

For all span element, You can try,

$('span');

For all spans having id started from select You can try

$("span[id^='select']");

For any element, You can try

$('*').not('#dontSelect, #dontSelect *')

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388326

I think an easier solution could be is to attach the handler to the document element, then check whether the event's target is inside the dontSelect element if so don't do anything

$(document).click(function (e) {
    if (!$(e.target).closest('#dontSelect')) {
        //do your stuff
    }
})

But if you want to target only span elements then

$('span:not(#dontSelect span)').click(function(){
    //your stuff
})

or

$(':not(#dontSelect, #dontSelect *)').click(function(){
    //your stuff
})

Upvotes: 1

Related Questions