I am trying to display Json Response in input checkbox tag on button click. But i am unable to do it.
Can anyone guide me how to do it.
Below is the code -
<!DOCTYPE html>
<html>
<body>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<label><input type="checkbox" name="test_name" value=""></label>
<button type="button" onclick="loadDoc()">Change Content</button>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementsByName('test_name').innerHTML = JSON.stringify(this.responseText,undefined, 2).replace(/\\n/g, "").replace(/\\r/g, "")
.replace(/\\/g, "")
.replace(/^"/, "")
.replace(/"$/, "");
var jsonobj = JSON.parse(document.getElementsByName('test_name').innerHTML);
for ( var i=0;i<jsonobj.length; i++)
{
console.log(jsonobj[i].Newsletter_Name);
var label = document.getElementsByName('test_name');
console.log(label);
label.value= jsonobj[i].Newsletter_Name;
};
}
};
xhttp.open("GET", "https://members.s7.exacttarget.com/Pages/Page.aspx?QS=38dfbe491fab00ea764e20685ddf905a854eb2c63c649afb00651f16b30a4189&brand_code=PE", true);
xhttp.send();
}
</script>
</body>
</html>
Json Return - There will be a lot of data. But i am displaying few.
[{
"Test_Name": "FOOD",
"Brand": "Test"
},
{
"Test_Name": "HOME",
"Brand": "Test"
}]
Sticking with jQuery only (read more here) - to tick the checkboxes from the values in your JSON, you can do something like this:
That is assuming your checkboxes look something like this: