1

I try to make a form with jquery 1.11.1, but the checkboxes do not work! which is the syntax error?

$(this + "input[name=''] :checked").each(function() {
totalSum += parseInt($(this).val());
});
2
  • 1
    you can't use this plus a string as a selector. and remove that space before :checked Commented Nov 25, 2014 at 1:05
  • 2
    [object HTMLDivElement] is the syntax error in the selector. this is a DOM element. What did you expect from concatenating a DOM element with a string? Commented Nov 25, 2014 at 1:06

2 Answers 2

2

You probably want to use something like this:

$( this ).find( "input[name='']:checked" ).each(function() {
  totalSum += parseInt($(this).val());
});

Using find() you can search for elements, which are descendants of a given element.


Anyway, are you sure you want to search for <input> elements, which have a name attribute set to an empty string? Because that is, what you are currently selecting.

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

1 Comment

Don't forget about $("input[name='']:checked",this), which is the same as your answer.
0

Just to be different...

var totalSum = $(this)
.find("input[name='']:checked")
.map(function() { return parseInt(this.value, 10); })
.get()
.reduce(function(total, value) { return total + value; }, 0);

2 Comments

What is the purpose of the get()? Which prototype does that come from?
@Sirko Turns it into a real array. It's on jQuery's, as I'm calling it directly on a collection.

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.