Ad

JSON: Fetch Id From Json While Redirecting Login.html To Homepage.html

- 1 answer

In given code, after login from index.html page to homepage.html,

for eg: name = Student
    password = 123

then in homepage.html body tag value should be like

<html>
<body id="student1">

this is home page.

</body>
</html>

which can be set from json file. If uname = controller then id should be "cntOne". Help me with some sort of code.

json:
{
"Student" : [ { "uName"  : "student1", "upass" : "123", "id":"student1" },], "controller":[{ "uName"  : "cntOne", "upass" : "123", "id":"cntOne"  },]    
}

**index.html:**

<input type="text"> 
<input type="password"> 
<button>Submit</button>

**homepage.html:**
<html>
<body id="">
    this is home page.
</body>
</html>
Ad

Answer

I've added a snippet here, and use alert to show the changed id of div.

var json = {
  "Student": [{
    "uName": "student1",
    "upass": "123",
    "id": "student1"
  }]
};
if (json["Student"]) {
  $('#myDiv').attr('id', json["Student"][0].id);
  alert($('div').first().attr('id'));
} else if (json["controller"]) {
  $('#myDiv').attr('id', json["controller"][0].id);
  alert($('div').first().attr('id'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv">

</div>

Ad
source: stackoverflow.com
Ad