Javascript quetion

Discussion in 'Programming' started by mumfry, May 17, 2012.

  1. #1
    is there any way to get a id that was created dynamically into a JavaScript function

    i know its possible to get a specified id using document.getElementById

    is there a function similar or a similar method for getting id on the fly

    any help would be greatly appreciated

    thanks
     
    mumfry, May 17, 2012 IP
  2. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #2
    Why won't "document.getElementById" work?
    Show us a bit of your code and what you're trying to do.
     
    HostPlanz, May 18, 2012 IP
  3. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    well

    the code goes like this

    the HTML

    these are created from items in a database

    <img id="label1" src="http://www.example.com" />

    <img id="label2" src="http://www.example.com" />

    <img id="label3" src="http://www.example.com" />

    i have no problem passing the id from the images onclick to the java code, the problem is getting it to be dynamic. document.getElementById would only work for the id you specified

    like below

    the java

    document.getElementById(label1)

    //because from what i understand you must specify an id when using document.getElementById... therefore it can never be dynamic it would always be the same. this code would only get the first id (label1) out of the three

    i also tried this

    document.getElementById(label1)
    document.getElementById(label2)
    document.getElementById(label3)
    this didnt make any difference, instead all it did now was get the id of the last image (label3). and never paid any attention to the first two calls
     
    mumfry, May 18, 2012 IP
  4. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #4
    What are you trying to do after you get the id?
    You need to store the values using an array

    
    [COLOR=#000000][FONT=courier new]var images=new Array(); [/FONT][/COLOR]
    [COLOR=#000000][FONT=courier new]for(i=0; i<3; i++){
    var increment = i+1;
    images[i]=[/FONT][/COLOR][COLOR=#111111]document.getElementById('label'+increment);[/COLOR][COLOR=#000000][FONT=courier new]
    }[/FONT][/COLOR]
    
    Code (markup):
    images[0] will be the first <img>
    images[1] will be the second <img>
    images[2] will be the third <img>
    .... and so on
     
    HostPlanz, May 18, 2012 IP
  5. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    so here is the original code inside the java file
    maybe yo understand it bettere than i do

    var button = document.getElementById('button');

    switch(button.name)
    {
    case 'button0':
    button.src = 'images/1.jpg';
    button.name = 'button1';
    break;
    case 'button1':
    button.src = 'images/0.jpg';
    button.name = 'button0';
    break;
    }


    can i use your code to do the same thing
     
    mumfry, May 18, 2012 IP
  6. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #6
    Still not understanding what you're trying to do.
     
    HostPlanz, May 18, 2012 IP