0

Here is my html:

<ul class="inboxList">
    <li data-selecttype='global|mailbox|inbox'>Inbox</li>
</ul>

And here is my jquery code:

$(".inboxList").children().each(function(child){
    console.log($(child).data("selecttype"));
});

I also tried taking out the $() in the child:

$(".inboxList").children().each(function(child){
    console.log(child.data("selecttype"));
});

But that didn't work.

My return values are null and undefined. I was expecting the return value to be global|mailbox|inbox What am I doing incorrectly?

Thanks for the assistance!

1
  • like PSL said, just use $(this). It'll make your life easier. Commented Jun 19, 2013 at 19:06

2 Answers 2

3

You are using the wrong argument from each's callback. You should use the second arg for the element or ditch the args and just use this. it should be:

$(".inboxList").children().each(function(i, child){ // each has 2 args first one is index and second one is the element.
    console.log($(child).data("selecttype")); //Or this

    console.log($(this).data('selecttype'));
});

http://jsfiddle.net/JUyHg/

.each( function(index, Element) )

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

Comments

0

Try this,

$(".inboxList").children().each(function(){
    console.log($(this).data("selecttype"));
});

According to the definition on jQuery's site, .each() takes 2 arguments, index and Element, where,

index - is the index of the current matched element in the jQuery object returned by .each()

And

Element - is the current element, in your case

<li data-selecttype=​"global|mailbox|inbox">​Inbox​</li>​

Read more about .each()

Fiddle

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.