0

I have the following code:

<div id="users">
<p><input type='checkbox' value= "1" checked>one</p>
<p><input type='checkbox' value= "2" checked>two</p>
<p><input type='checkbox' value= "3" checked>three</p>
</div>

I need to remove the paragraph within my <div id="users"> that contains checkbox value="2" using jQuery.

I know the values checkbox value=2 and <div id="users"> and also know its inside<p>.

0

4 Answers 4

3

do as follows:

$('#users').find('input[value="2"]').parent().remove();

This code will find an input with value 2 inside #users, and then get the parent of that input (your paragraph), and remove it from the DOM.

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

3 Comments

@KevinBoucher, it works. The selector argument is optional since it's in square brackets.
$("#users p input[value='2']").parent().remove(); works also
@Maybe yes, but as per jQuery docs, traversing (find) is faster.
2

You can do this succinctly by using jQuery's has() method:

$('#users p').has('[value="2"]').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="users">
  <p><input type='checkbox' value="1" checked>one</p>
  <p><input type='checkbox' value="2" checked>two</p>
  <p><input type='checkbox' value="3" checked>three</p>
</div>

1 Comment

nice. a bit different from other answers plus my answer
1

Here how it's done

$(document).ready(function() {
  $("input[value=2]").parent().remove();

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="users">
  <p>
    <input type='checkbox' value="1" checked>one</p>
  <p>
    <input type='checkbox' value="2" checked>two</p>
  <p>
    <input type='checkbox' value="3" checked>three</p>
</div>

Comments

0

I think you want this:

$('#users input[value="2"]').parent().remove();

1 Comment

btw that won't work. Even if you correct it as $('#users p input').remove('input[value="2"]'); it will remove input elements, he wants remove p element UPD: Too fast editing :)

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.