0

I'd like to pull an attribute value from the input

This is some of my html:

<input type="checkbox" name="invoice" client="14" value="53">
<input type="checkbox" name="invoice" client="14" value="54">
<input type="checkbox" name="invoice" client="17" value="52">

But I can't seem to be able to select anything, everytime I try to use a function while I'm eaching through them, is throwing undefined function

var inputs = $('input[name=invoice]');
    inputs.each(function(){
        console.log(this.attr('client'));
    });

Uncaught TypeError: undefined is not a function

while if I say this.value it returns 53, 54 and 52

9
  • 3
    You made a typo, use $(this) instead of this . You want to access the jQuery element, not the dom element which have not the .attr method Commented Oct 22, 2014 at 18:41
  • 3
    this === HTMLElement, .attr() === jQuery method. HTMLElement doesnt have access to the jQuery method. Use $(this).attr('client') Commented Oct 22, 2014 at 18:42
  • 1
    Awesome, that's it, didn't knew that! Commented Oct 22, 2014 at 18:43
  • All jQuery variable are written with a $ Commented Oct 22, 2014 at 18:44
  • 2
    Also this.getAttribute('client') Commented Oct 22, 2014 at 18:44

1 Answer 1

1

attr() is a jQuery method. this inside .each() refers to the native HTML element. You should make it a jquery object for calling jQuery methods like $(this).attr('client')

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

9 Comments

Already said in comment, already tried by OP, are you trying to get easy points ?
@Superdrac Questions should not be answered in comments, that means they'll be left unanswered, new users will keep coming to it thinking the question is unanswered... Comments are meant for different purpose.
Both of you are right, question should be answered but when someone answered the question in the comment (and it work) you should A) Let him answer and get the point or B) Answer it yourself and checking the community wiki on the bottom right of the answer box. Related : meta.stackoverflow.com/questions/251597/…
@Superdrac Knowing that this question is a duplicate, I would either find the duplicate or make it community because it is really easy points.
@TJ TLDR; im sorry ahah! But I quit this quest when i couldn't find one on the first page on google. Here, have an upvote but i would really like to see that question a community wiki ;)
|

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.