0

For starts I have this HTML with example data within it:

<div class="card {toggle:'someinformation'}">               
    <div class="off"></div>                 
    <div class="on {toggle:'getThisInfo'}"></div>
</div>

I want to use jQuery and retrieve this information. Upon asking someone they told me to use the follow (which did not work):

    var $card = $(this);
    var this_card = $card.children(".on").metadata()["toggle"];

and was told the value of "this_card" would be 'getThisInfo' -- which it did not work. And yes, I did make sure metadata was included in HTML and jQuery as well.

Any other suggestions?

2
  • 1
    jQuery doesn't have a metadata() method. Are you using this plugin: github.com/jquery-orphans/jquery-metadata Commented Sep 8, 2013 at 23:05
  • That looks like a templating language, maybe you should include all the information in your question ? Commented Sep 8, 2013 at 23:06

1 Answer 1

3

Just use the HTML5 data-* attribute:

<div class="card" data-toggle="someinformation">               
    <div class="off"></div>                 
    <div class="on" data-toggle="getThisInfo"></div>
</div>

Then access it using jQuery's .data() function, like this:

var $card = $(this),
    this_card = $card.children(".on").data("toggle"); // "getThisInfo"

Update:

Based on the extra code posted this should work perfectly for you:

var $on_cards = $(".card > .on:visible");
if ($on_cards.length == 2) {
    var $card = $('.card:first'), // was $(this), replaced just for this demo
        this_card = $card.children(".on").data("toggle"),
        $matched_cards = $on_cards.filter('[data-toggle="' + this_card + '"]'),
        event_name = "no_match";
    if ($matched_cards.length === 2) {
        event_name = "found_match";
    }
}

This uses jQuery's Attribute Equals Selector to find .on elements with the same data-toggle value.

Important: if you need to change the .on's value then don't use jQuery's .data() function to do so, use:

$(selector).attr('data-toggle','your-new-value');

This is because .attr() and .prop() update the attributes in the HTML (which the [data-toggle=""] selector checks) whereas .data() updates an internal jQuery object without affecting the actual element.

Here it is working: http://jsfiddle.net/c4fQ3/1/ (try changing the .on element's data-toggle value)

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

10 Comments

And for extra JSON fun: "When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string."
Thank you so much Joe, but say this is for a matching game and I wanted to check how many times the data occurs between the two cards would I simply do "this_card.length == 2" ?
@Arbitrary - no problem. Are there two instances of .card or would you just be comparing the data of .off and .on?
There would be two instances of ".card"
@Arbitrary - I've just added a solution for you. I've checked it in the fiddle and it should solve your problem!
|

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.