0

I try to find all elements whith a particular attribute. For each elements, I want to do something dependant of the value of the attribute.

I did a test to take back the value of the attribute but it doesn't work and I don't see the problem.

HTML:

<div id="t">Test</div>
<div data-test="One">Value One</div>
<div data-test="Two">Value Two</div>

JS:

$("[data-test]").each(function(){
    $("#t").append(this.attr("data-test"));
});

JsFiddle link: http://jsfiddle.net/fbue5dpc/

Thank you!

4 Answers 4

4

this in your each block refers to the DOM element which does not have the attr method. You should use $(this) to convert it to a jQuery object. You can also improve this further by using the data method to retrieve the values and caching the #t element to reduce the number of DOM retrievals being made:

var $t = $('#t')
$("[data-test]").each(function(){
    $t.append($(this).data('test'));
});

Updated fiddle

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

1 Comment

Thank you for the improvement.
2
var elemnt = $("[data-test]"),frag = "";

elemnt.each(function () {
    frag += $(this).attr("data-test");
});
$("#t").append(frag);

Working DEMO

6 Comments

You either should not change the selector or you should explain why you changed [data-test] to div[data-test].
t.niese y dont u give answer rather than commenting and judging others answers ..let it be made by OP...
For the div, I understand the logic. However in my app, the data-test attribute can be on different elements (h1, p, ...)
@Vaibs_Cool why should I add an additional answer if RoryMcCrossan already gave a perfectly fine answer with an explanation. SO is not only a place where people get help as individuals, but ideally is a place to help others in future with similar question. So whenever the code of the OP is changed it might look like it is important to solve the problem. I rarely give down-votes. But I always add an comment if it believe a part of the answer can be improved.
@RoryMcCrossan Its not bad to comment on others answers but only commenting on others is what i felt strange..and that too bit out of box comments... anyways cheers for your correct answer..
|
1

Try,

$(this).attr("data-test")

Explanation: inside the foreach loop you will get dom element since you are iterating list of dom element. So you cant invoke jquery method directly from dom elemet to do this you have to convert it into a jquery object to do this just use $(this)

3 Comments

Try... answers are not great. As you don't explain what you do and why it is necessary.
Thanks, I added some explanation
Thank you for the explanation. It works as expected.
0
$("div[id!='t']").each(function(){
    $("#t").append($(this).data('data-test'));
}

5 Comments

$("div[id!='t']") ...?
The question is about I try to find all elements with a particular attribute..
as he wants to add the data-test property only. So this each loop will select onlythose dive which dont have id='t' cz he is adding the attribute value in $("t") innnerHTML.
But it changes the meaning of the question. And in addition $(this).data('data-test') won't work anyway, it should be either $(this).attr('data-test') or $(this).data('test').
It is just an example. It will be integrated in a much more big file with different id's. So I don't think it is a good way.

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.