2

This question has been asked before, but I can't find any answers that have been given since May 2010. If one or more of these answers are still relavent, I'd be happy to delete this question.

Does jQuery have a selector that will allow me to filter by values added by .data()?

Example: (see comments)

//Set IsValid data attribute on all elements that have fired the blur event
$(".txtPayment").blur(function (e) {
    if (IsCurrency($(this).val()))
        $(this).data("IsValid", true);
    else
        $(this).data("IsValid", false);

    SumPayment();
});

//Sum all payments

// I'd like to be able to select all inputs with:
// class == "txtPayment" value != "" && IsValid == true
$('.txtPayment[value!=""]').each(function ()
{
    var sum = 0;

    if ($(this).data("IsValid"))
    {
        sum += Number($(this).val());
        $(this).removeClass("textError");
    }
    else
    {
        $(this).addClass("textError");
        $("#spanSumPayment").addClass("textError");
    }
});

Is this possible without plugins?

1

5 Answers 5

4

Does jQuery have a selector that will allow me to filter by values added by .data()?

Almost. It has a function to do this.

$(".txtPayment").blur(function () {
    $(this).data("IsValid", IsCurrency($(this).val()) );
    SumPayment();
});

var validOnes = $('.txtPayment[value!=""]').filter(function() {
  return !!$(this).data("IsValid"));  // (!! turns anything into a Boolean)
});
Sign up to request clarification or add additional context in comments.

6 Comments

Nicely done, thanks. Thanks also for the optimization of my original code - concise and readable.
@JamesHill FYI You could also do things like $('.txtPayment[value!=""]').toggleClass("textError", function() { return !!!$(this).data("IsValid"); });
@Tomalak why would you negate it three times instead of just once?
@Kevin: Because... why, you are right. That makes no sense. :) I'll correct it (damn, if only I could edit my older comments). — !$(this).data("IsValid"); is enough, of course.
@tomalak I just thought maybe you knew something I didn't, :p
|
1

No. Why don't you want to use a plugin? You could always do something like this, using .filter:

$('.txtPayment[value!=""]').filter(function() {
    return $(this).data("IsValid");
}).each(function() { ... });

1 Comment

I have nothing against plugins. I put that in my OP because I had already found a plugin to do it and I wanted to see if there was a "native" way to do it before I used a plugin.
1

You can select by attributes that existed when the page loaded, such as data-id="5", however, not by data set with .data(). You can however use the .filter() method for both.

$('.txtPayment[value!=""]').filter(function(){
  return $(this).data("isValid");
})

Comments

1

No. If it's not listed here, it can't be used as a selector.

If you'd like to store all the elements with a certain piece of data in a variable, you can use filter(), for example:

var dataElements = $('.txtPayment[value!=""]').filter(function(){
    return $(this).data("IsValid");
});

Comments

0

You can alternatively manually set the data attributes of an element like this in jQuery:

$(element).attr('data-test', true);

This will allow you to either access data with the data() function, like this:

$(element).data('test'); ==> true

While allowing you to select the element in jQuery/CSS like this:

  • element[data-test] - will only check for the data-test attribute's presence
  • element[data-test="true"] - will also apply a condition to the attribute selection

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.