SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    returning innerHTML from a class

    I've been trying to learn more about the innerHTML property of Javascript for almost two years. I don't see it used much, however, a previous colleague of mine wrote an entire webform in javascript using innerHTML and I was very impressed. She has since left the company, so I can't ask her. My question is I have all addresses listed with class="address". I want to write a function, (or functions), that will return the inner html for all addresses as elements in an array.

    A code snippet to help me get started is appreciated.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    About innerHTML, it is standard policy to avoid it unless at all possible.
    It's possible also to write clever code using nothing but eval(), {shudder}, but that doesn't mean that it's a good idea.

    About getting the elements by class name, there are plenty of functions to getElementsByClassName so go find one and use it.

    Once you have your list of elements you can use

    Code Javascript:
    function addressInnerHTML(elems) {
        var i;
        for (i = 0; i < elems.length; i++) {
            elems[i] = elems[i].innerHTML;
        }
        return elems;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks, PW. I agree, using innerHTML isn't the best way, (which is probably why I haven't seen it used much). I just always wanted to understand how to use it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •