1

What I'm trying to do is, to change i's class (which located inside button) on click

<button><i class="fa fa-square-o"></i></button>

Using js code like below (inside on click function):

...
$(this).child("i.fa").removeClass("fa-square-o");
...

getting child is not a function error

What am I doing wrong?

2
  • Maybe try $(this + " > i").removeClass("fa-square-o) ... ? or $(this).find("i").removeClass("fa-square-o) Commented Nov 9, 2014 at 18:12
  • Did you mean children instead of child? Commented Nov 9, 2014 at 18:14

5 Answers 5

5

Yes, really there is no child() method in jQuery.

You may use find() or children() for that.

$(this).children("i.fa").removeClass("fa-square-o");
Sign up to request clarification or add additional context in comments.

Comments

2

You could use find() instead.

$('button').click(function() {
    $(this).find("i.fa").removeClass("fa-square-o");
});

Fiddle

Comments

1

child is not a function. try this function:

$(this).children("i.fa").removeClass("fa-square-o");

Comments

0

jQuery accepts a second argument as context:

$('button').click(function() {
    $('i.fa', this).removeClass('fa-square-o');
});

Comments

0

You are looking for jQuery .children() function. If you are trying to call that function from onclick attribute, you have to remember to pass reference to object by using "this" word.

<button onclick="removeClass(this)"><i class="fa fa-square-o">test</i></button>

removeClass = function(obj){

     $(obj).children("i.fa").removeClass("fa-square-o");

}

Working fiddle: http://jsfiddle.net/4wvgvzL9/

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.