views:

346

answers:

2

Couldn't seem to find any info about this googling. Hope that doesn't mean because you can't do it. I'm trying to check if metadata exists on an element. So if I have an element like <a href="#" class="myclass {myid:2,text:bla}">, with my click function I would use $(this).hasClass('myid') or $(this).hasClass({'myid'}). Is it possible to do this?

Also can you add metadata dynamically? If I try $(this).addClass({some:data}), it adds it onto the class, but I can't read it with metadata().

+1  A: 

As far as I can tell, the metadata extracted by that plugin is read-only, and it also caches the object it constructs so that it doesn't have to re-parse attribute values and such. If you want a read-write solution, it wouldn't be too hard to write a little function that would take the metadata extracted, and add it to the standard jQuery data store. That, then, can be modified at will. (Actually, I'd be sort of surprised if someone hadn't already written such a plugin.)

Sixten Otto
Is this what you meant: http://www.bennadel.com/blog/1518-jQuery-metaData-Plugin-Leverages-data-Method-jQuery-Plugin-Example-.htmHave no idea how to use it though.
Roger
+2  A: 

Sounds like you want to use jQuery.data(name, [value]);

You can store and retrieve data on any element using it like this.

//store values on the element
$('#someID').data('secret','My voice is my password, verify me.');
$('#someID').data('age',33);
$('#someID').data('name','Jasper');
$('#someID').data('occupation','Toy maker');
//retrieve values from the element
alert(
  'Hi my name is ' + $('#someID').data('name') +
  ' i\'m ' + $('#someID').data('age') +
  ' i\'m a ' + $('#someID').data('occupation') +
  ' oh, and ' + $('#someID').data('secret')
);
scunliffe
Hmm, the situation I have is that I have multiple elements with the same class. Originally I was using id's like: xx-1-1, xx-1-2, xx-1-3. The xx is just filler since id's can't start with numbers. The 2nd value between the dashes is my id, and the last values are there just so the id's are unique. Now I am using metadata {myid:1} instead. So I might have 15 elements with class="myclass" and 5 of them would be {myid:1}, another 5 {myid:2}, and the last 5 are {myid:3}. Say I want to use addClass() on the classes that have metadata of myid:2, how would I do that?
Roger
I'm very confused. Ignore the IDs and Classes etc. what is it that you are trying to accomplish? Based on your very last statement, it sounds like you want to add a CSS class, to a "set" of elements. There are 2 easy ways to do this, either (A) add a class (you can have several) to all the items in the set.. e.g. "group1" (it doesn't have to have any styles set for it), then just use $('.group1').addClass('special'); Option (B), use .data() to add info to each element in your "set", then later query every element you care about to see if it has the data set. Option (A) is IMHO the best.
scunliffe
Sorry, I guess I was being unclear. Basically I wanted to use addClass, removeClass, hasClass, etc. on metadata, but it seems you cannot. Or see if there were special functions such as addMetadata, but it looks like there isn't as well.
Roger