Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    rf9
    rf9 is offline
    New Coder
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    getElementbyId targetting div

    Hello, first post here and also fairly new to javascript so please excuse any lameness. I'm using my Actionscript knowledge as the basis for my javascript, they seem pretty similar.

    I'm trying to select a div that has been generated by javascript. I'm not sure where I'm going wrong. I have 5 divs named title0, title1 and so on and I want to target one using the variable i.

    The last line does not work, it returns null or no properties, maybe my syntax is wrong. I kinda made it up
    Code:
    var i = 0;
    var setProperties = function() {
         alert(i); // this works
         var userInput = document.getElementById('element_label').value;
         document.getElementById('title' + [i]).innerHTML = userInput;
    }
    If I change the last line to this then the targetting works:
    Code:
    document.getElementById('title' + 1).innerHTML = userInput;
    Any advice much appreciated.
    Last edited by rf9; 06-16-2008 at 11:57 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    try
    Code:
    for(var i=0;i<5;i++)
     document.getElementById('title'+i).innerHTML = 'somtext'+i;
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    rf9
    rf9 is offline
    New Coder
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks abduraooft,
    The code you posted does work the only problem is it changes the text for all 5 divs. My fault as I didn't really give enough explanation in my first post.

    I only want to change the text for one div at a time so if 'i' is 3 then the text for title3 will be updated and so on.

    Thanks

    edit: just noticed an error in my first post ('title' = [i]) should read ('title' + [i]). Original post now corrected.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    var i = 0;
    var setProperties = function() {
    alert(i); // this works
    var userInput = document.getElementById('element_label').value;
    document.getElementById('title' + [i]).innerHTML = userInput;
    }
    It should be
    Code:
    document.getElementById('title' +i).innerHTML = userInput;
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Which is the event and when should that function be triggered? Can you post the HTML code as well?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    rf9
    rf9 is offline
    New Coder
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the replies, I figured out a solution now so I thought I'd post it.
    Code:
    //Set value to pass to setProperties function
    var showControlPanel = function(count) {
    	document.getElementById('element_label').setAttribute('onKeyup', 'setProperties('+count+')');
    }
    Code:
    // count used for increment
    var setProperties = function(count) {
    	var userInput = $('element_label').value;
    	document.getElementById('title'+count).innerHTML = userInput;
    }
    Thanks


  •  

    Posting Permissions

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