1

I need to remove a tags with specific attribute value from a div tag.

This is for example:

<div id='testdiv'>
    <a dir='hello' href='#' ></a>
    <a dir='how' href='#' ></a>
    <a dir='which' href='#' ></a>
</div>
<input type='button' id='btn' />

Here is my jQuery:

$('#btn').click(function(){
   if($("#testdiv").find("a[dir='hello']").length == 1)
   {
       $("#testdiv").remove("a[dir=hello]");
   }
}

But its not working. What change shall I need to do with my jQuery?

3
  • Whats the question ? this code is not working as intended ? Commented Jun 5, 2013 at 12:05
  • dir isn't a valid attribute for an anchor. If you need to use custom attributes, check out html5 data attributes Commented Jun 5, 2013 at 12:11
  • I hope this can help you understand the functionality better jsfiddle.net/cVb5h Commented Jun 5, 2013 at 12:20

4 Answers 4

1

try this:

$("a[dir=hello]").remove();

Here's the documentation

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

Comments

1

It's working:

$(function(){
        $('#btn').click(function(){
                if($("#testdiv").find("a[dir='hello']").length=='1'){
                    $("#testdiv").find("a[dir='hello']").remove();
                }
        });
    });

Comments

0
$('#btn').click(function(){
   $("#testdiv").remove("a[dir='hello']");
})
  • You need no if.
  • You forgot the quotes in the remove statement.
  • You forgot the last ).

Comments

0

Try this:

$('a[dir="hello"]').remove

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.