Sangam Uprety
Sangam Uprety

Reputation: 1482

Parse string value pair in javascript

I have an attribute of a list object in html like this:

<ul>
<li id="myID" add-param="{"type1":"myType1","type2":"myType2"}">Test</li>
</ul>

Having the list item with specified ID, how could I get the value myType1,myType2 by the key type, type2 ?

Thanks.

Upvotes: 0

Views: 641

Answers (1)

Dogbert
Dogbert

Reputation: 222358

var json = JSON.parse($('#myID').attr('add-param'));

alert(json.type1) # myType1

Some old browsers don't have JSON.parse, you'll need to use this - https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Sidenote: You should be using data attributes instead of defining your own custom attributes - http://ejohn.org/blog/html-5-data-attributes/

Upvotes: 3

Related Questions