Web Analytics Made Easy -
StatCounter can javascript update the value of an element that isn't a form input? - CodingForum

Announcement

Collapse
No announcement yet.

can javascript update the value of an element that isn't a form input?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • can javascript update the value of an element that isn't a form input?

    Hello,

    I'm using Javascript to create some dynamic content on my website. Basically it's looping through some values and updating an input field with the values.

    What I'm wondering is, do I have to do this using a form and an input field? Is there a way I can get Javascript to update the content inside a DIV for example?

    Thanks,

    Nicky

  • #2
    yes you can use innerHTML:
    https://developer.mozilla.org/En/DOM/Element.innerHTML

    Comment


    • #3
      You can use the innerHTML property. This is non-standard, but works in all modern browsers. If you have a DIV with id="change", then the Javascript
      Code:
      div#change.innerHTML="New content for div change";
      will have the desired effect. You can insert any string value here, or anything that will automatically convert to a string.

      John

      Comment


      • #4
        It depends on.
        First, it depends on what do you mean by "the content inside a DIV". Is it about a textNode? Tag nodes? Mixture of text and tags?

        innerHTML is the simplest, but, as Actinia pointed out, innerHTML is not a DOM standard method. If your aim is only to display some "inactive" content, it will do the job. But if you need to insert some elements and you need to handle them later via javascript as part of the DOM tree, you must use DOM methods to do that:

        More about DOM and DOM methods:
        http://www.w3schools.com/js/js_obj_htmldom.asp
        https://developer.mozilla.org/en/The_DOM_and_JavaScript
        http://www.howtocreate.co.uk/tutoria...t/domstructure
        http://www.w3.org/DOM/#what
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment

        Working...
        X