JSON problem

Posted on 2012-09-22
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 JSON

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

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

Open in new window

Question by:Moizsaif123
    LVL 42

    Expert Comment

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


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

    Author Comment

    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>
    <h2>Create Object from JSON String</h2>
    First Name: <span id="fname"></span><br /> 
    Last Name: <span id="lname"></span><br /> 
    var txt = '{"employees":[' +
    '{"firstName":"John","lastName":"Doe" },' +
    '{"firstName":"Anna","lastName":"Smith" },' +
    '{"firstName":"Peter","lastName":"Jones" }]}';
    var obj = eval ("(" + txt + ")");

    Open in new window

    LVL 82

    Accepted Solution

    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.
    LVL 81

    Assisted Solution

    > 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 :

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
    Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
    The viewer will learn how to dynamically set the form action using jQuery.
    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…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now