senty senty - 1 year ago 81
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download