0

I have an array with some HTML in it, so something like:

var array=[
           "<div anAttribute=19 class='foo'>This is the content of the foo div.</div>", 
           "<div anAttribute=14 class='bar'>This is the content of the bar div.</div>"
          ];

I want to get the value of anAttribute for both of them.

array[0].$('.foo').attr("anAttribute");

Would return 19.

3 Answers 3

2

You can do this way:

$(array[0]).attr("anAttribute");
$(array[1]).attr("anAttribute");

I am assuming anAttribute is a valid html attribute, if not prefix it with data-*.

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

2 Comments

You can make up custom attributes like that in HTML5. Thank you for your answer, I'll accept it ASAP!
@ebol4 yes custom attributes must be prefixed with data-* in HTML 5
1

This will give you the attributes in an array:

var attributes = $(array.join('')).map(function(){
  return $(this).attr('anAtribute');
}).get();

Then you can access them like:

attributes[0] //= 19
attributes[1] //= 14

Comments

1

you just need to wrap the string in the jquery closure:

jQuery(array[0]).attr('anAttribute');

will return 19

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.