SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble with document.getElementById

    I want to update (clear the HTML out of) DIVs where the DIVs ID is generated by the content. I am having trouble putting a generated ID into document.getElementById(---myGeneratedID goes here---).innerHTML .

    Code:
    var  productNum = 9960353; 		
    var button = 'cell' + productNum;
    var clearHTML = ' ';		
    document.getElementById(button).innerHTML = clearHTML;
    With the example above, in effect, I would like it to be the same as:
    Code:
    document.getElementById(cell9960353).innerHTML = clearHTML;
    Any Suggestions?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Id should be a string and nothing else

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi billiam,

    I don't really get the question. Do you have a test page online so we can take a look at it?

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me try to clarify. I want to put the value of a variable in document.getElementById(myVariable).innerHTML = clearHTML;

    This variable will be a string.

    Code:
    var myVariable = "cat";
    var clearHTML = ' ';		
    document.getElementById(myVariable).innerHTML = clearHTML;
    So the above code would do the same thing as:
    Code:
    document.getElementById('cat').innerHTML = clearHTML;
    Is this possible, I cannot get it to work.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how are you calling this code, in a function? Show more code, otherwise it will be impossible to hlelp you.

  6. #6
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree, the code you have shown should work perfectly as far as I know, there has to be a problem elsewhere...

    Regards,
    Jordan

  7. #7
    Non-Member deathshadow's Avatar
    Join Date
    Jul 2006
    Location
    Dublin, NH
    Posts
    901
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This might sounds like a silly question... but what browser?

    I know this is excessive for backwards compatability (all the way back to NS4 - I keep meaning to trim that out) but I still don't wholly TRUST getElementByID.

    Which is why I have this function as part of my library:
    Code:
    function Get_Object(name) {
      if (document.getElementById) {
        return document.getElementById(name);
      } else if (document.all) {
        return document.all[name];
      } else if (document.layers) {
        if (document.layers[name]) {
          return document.layers[name];
        } else {
          return document.layers.testP.layers[name];
        }
      }
    }
    Although like the others, I do agree that in any modern browser your code should work as is... leading me to think the problem is elsewhere. Are you SURE you have an element with that ID assigned to 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
  •