0

When i did the "inspect Element" the HTML code i got is,

    <domain-picker class="pull-right" 
current="{"label":"AMN/GRP","value":"assf2324234"}" in-header="true" show-in-header="true">
    </domain-picker>

Could anyone please let me know in jquery

  1. how to get the value "assf2324234" of the above "domain-picker" element.
  2. how to set with new string for the "value" attribute of the above "domain-picker" element
2
  • 3
    current value will not contain whole json value as you are displaying, it only contain { as this value inside first "" other part will not be the value of current. Commented Dec 28, 2016 at 6:35
  • @Rishi must have been OK for OP to have copied from dev tools live html. The quotes would be minor issue....browser would have not output proper html otherwise Commented Dec 28, 2016 at 6:36

4 Answers 4

6

Following is commented to show steps

// target element
var $picker = $('domain-picker'),
 // parse current attribute value string to object
current= JSON.parse( $picker.attr('current'));
// change value
current.value ='someOtherString';
// stringify object and put back as attribute value
$picker.attr('current', JSON.stringify(current));
Sign up to request clarification or add additional context in comments.

Comments

3

Modify your HTML, Use single quotes for current attribute

<domain-picker class="pull-right" 
current='{"label":"AMN/GRP","value":"assf2324234"}' in-header="true" show-in-header="true">
</domain-picker>

in jQuery

var json = $("domain-picker").attr("current");
json = JSON.parse(json)
var value = json.value;

Here is working demo https://jsfiddle.net/758y0fp1/1/

Comments

0

As @charlietfl mentioned, if you wanna use jQuery, his answer is the 1 you need, this can be done with pure Javascript too, ie.

// getting the element
var picker = document.getElementsByTagName('domain-picker')[0];
// parsing current attribute string to object
var current= JSON.parse(picker.getAttribute('current'));
// changing the value
current.value ='newString';
// stringify the object and set the attribute again like
picker.setAttribute('current', JSON.stringify(current));

Comments

0

You need to first get the value of attribute .attr() in jquery or getAttribute() in javascript will help. Now since the value is an invalid JSON(as per the html provided by you), convert the invalid JSON to valid and then select the value.

var obj = document.querySelector("domain-picker").getAttribute("current");
var json = obj.replace((/'/g), "\"");
var obj = JSON.parse(json)

//console log the retrieved value.
console.log("value :", obj["value"])

// set attribute now

obj["value"] = "1234567";


document.querySelector("domain-picker").setAttribute('current', JSON.stringify(obj));
<domain-picker class="pull-right" 
current="{'label':'AMN/GRP','value':'assf2324234'}" in-header="true" show-in-header="true">
  Hello inpsect me to see the changed attribute value.
    </domain-picker>

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.