0

I'm trying to find an element by it's data-id and data-type so I can do stuff with it, but I'm finding an error when doing so.. Here's my function:

    find : function(id, type) {
    $('.elem').each(function(index, element) {
        if ($(this).data('id') == id && $(this).data('type') == type)
            return $(this);
    });
},

What I try to do for example is:

myClass.find(1, 'myType').text('whatever');

How can I do this?

EDIT: Thanks to adeneo and user3558931! Modified to:

    findPro : function(id, type) {
    return $('.elem[data-id=' + id + '][data-type=' + type + ']');
},
  • 1
    In `.each()` `return` does not do what you think it is doing!! :( – PeterKA Jun 10 '14 at 23:34
  • 1
    You could to var elements = $("input[data-type='myType']"); elements.each(function(idx,elem){$(this).text('whatever');}); I'll try to re-write your function just as you have it though. – IrishGeek82 Jun 10 '14 at 23:36
  • 1
    Also: http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value , this might be a duplicated questions. – IrishGeek82 Jun 10 '14 at 23:40

2 Answers2

3
find : function(id, type) {
    return $('.elem[data-id="'+id+'"][data-type="'+type+'"]');
},

as a sidenote, this won't work if the data was initially set with data(), I'm assuming these are HTML5 data attributes, as in

<div data-id="something" data-type="something else"></div>

and this is not chainable as it's not added to jQuery's prototype

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • That doesn't short-circuit. I doubt two elements will have the same id. The OP's intent was to return a single element as well as far as I can see. – plalx Jun 10 '14 at 23:42
  • @plalx - this is exactly what the OP is doing, and why wouldn't two elements have two different ***DATA*** attributes. – adeneo Jun 10 '14 at 23:43
  • 1
    It's rather rare in any domain that an `id` field would be used for two different entities. I find it also a safe assumption that there will not be a duplicate view of the same entity in the current context. Therefore, the OP must be interested in a single element. You can also see that `return $(this);` taken from the OP's code communicates this intent, even if it doesn't do what he thought it would. – plalx Jun 10 '14 at 23:46
  • @plalx - I have no idea what you're talking about, these are ***DATA ATTRIBUTES***, you can have as many as you want, and they can be duplicates, and if there's only one element with that data-id and data-type attribute, it only matches one element ? – adeneo Jun 10 '14 at 23:47
  • Nevermind, you do not get my point and it doesn't really matter afterall. Your'e focused on HTML rules while that's not what I'm pointing out. I'm speaking about the role an `id` field usually plays in a domain. Why would there be multiple identical views of the same entity? – plalx Jun 10 '14 at 23:52
  • @plalx - No, I'm not really getting it, why would it matter if there are one or a thousand elements with that data-id, and this is HTML, who said anything about domains, views or entities ? – adeneo Jun 10 '14 at 23:55
1

In .each() return serves a completely different purpose. So you have to take a different approach:

......
findPro : function(id, type) {
    return $('.elem[data-id=' + id + '][data-type=' + type + ']');
},
.......
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • Actually, the function name is findPro, I just changed it to make it more general for posting and the original find function completely slipped my mind. – Sebastian Salines Jun 10 '14 at 23:41
  • @SebastianSalines I have edited the answer to change the name of the function and shortened the function body. – PeterKA Jun 10 '14 at 23:44
  • @adeneo I saw your answer after I edited mine; I did not copy - I would not copy. – PeterKA Jun 10 '14 at 23:47