1
<div class="test mypro demo-class" id="test-1">test</div>
<div class="test mypro demo-class" id="test-3">test</div>
<div class="test mypro demo-class" id="test-4">test 2 no demo class</div>
<div class="test mypro" id="test-2">test 2 no demo class</div>

in the above code i want to remove the div with id#test-2 with an additional condition check of div should not have .demo-class i tried this

$("#test-2 div:not(.demo-class)").remove();

please help thanks in advance

1
  • You can't have two same ID's. Commented Jan 18, 2014 at 11:24

2 Answers 2

1

try this:

$("#test-2").not('.demo-class').remove();

but I reccomend you to use unique ID use classes instead!

http://api.jquery.com/not/

Sign up to request clarification or add additional context in comments.

3 Comments

yes i use unique id in my code ,here i gave for some example code
if ID is unique is no-sense to check if has a class or not because exist only this element with that id @KarthickKumarGanesh
i my application i dynamically replace id based on the class
0

Currently, you've two elements with id test-2, chane it to class:

<div class="test mypro demo-class test-2">test 2 no demo class</div>
<div class="test mypro test-2">test 2 no demo class</div>

and then use:

$(".test-2:not(.demo-class)").remove()

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.