[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSON problem

Posted on 2012-09-22
4
Medium Priority
?
339 Views
Last Modified: 2012-09-22
Code below uses innerHTML... and I want to know why cant use just a simple .value to display text of a span or div tag...in JSON

<p>
First Name: <span id="fname"></span><br />
Last Name: <span id="lname"></span><br />
</p>

<script type="text/javascript">
document.getElementById("fname").innerHTML = obj.employees[1].firstName 

Open in new window

0
Comment
Question by:Moizsaif123
4 Comments
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 38424814
In jQuery, do it like this:

$('#fname').text(obj.employees[1].firstName);

Not really sure what your question is. You haven't told us anything about obj
0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 38424916
oops ya, heres the whole code, no actually i wanted to know the difference btw innerhtml and doc.getelementbyid('').value


also one more question, using ' quotes above vs " . are both applicable because sometimes i see either of the notation, dont know which one is in jquery or js

<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p>
First Name: <span id="fname"></span><br /> 
Last Name: <span id="lname"></span><br /> 
</p> 
<script>
var txt = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';

var obj = eval ("(" + txt + ")");

document.getElementById("fname").innerHTML=obj.employees[1].firstName 
document.getElementById("lname").innerHTML=obj.employees[1].lastName 
</script>
</body>
</html>

Open in new window

0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 1000 total points
ID: 38425111
Some elements like <div> only have 'innerHTML' and no 'value' while others like <input> only have 'value' without 'innerHTML'.  You have to use the one that is appropriate.

Single and double quotes are generally interchangeable in HTML and javascript except when one is used inside the other like in your JSON for txt.   Then the order is important.
0
 
LVL 83

Assisted Solution

by:leakim971
leakim971 earned 1000 total points
ID: 38425119
> i wanted to know the difference btw innerHTML and document.getElementById'').value

innerHTML if for div, span, and others containers
value is for form fields : input, select, textarea and select

' or " ? Both apply for JS or jQuery :
Check Quotes section inside String here : http://www.quirksmode.org/js/strings.html
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question