senty senty - 5 months ago 20
HTML Question

extracting attributes from html using jquery

I have a string that I got from outerHTML. It logs like:

var responseHtml = jQuery(data).find("#response")[0].outerHTML;


<div id="response" hello="world" big="span" hey="there">&lt;\/div&gt;"
<div class="clear"></div></div>

I am trying to get it in a format so I can access 'world' like:
values["hello"] => "world"

I am stuck here. What is the appropriate way to achieve it?


Rather than creating invalid attributes, you should be using the data attribute like:

<div id="response" data-hello="world" data-big="span" data-hey="there"></div>

And then to get those values with jquery, you can do:

var response = $('#response');'hello');  // returns "world"'big');  // returns "span"'hey'); // returns "there"

JS Fiddle