How do I change HTML objects with my JS strings ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • H

    How do I change HTML objects with my JS strings ?

    Ok, heres the deal ;

    I have my script code like this

    var v = "place";
    for (var i = 0 ; i < 5 ; i++)
    {
    v = v + i; // or v = "place" +i;
    // Here I want to access the resource in the HTML doc, whos id="place1",
    id="place2", etc etc, so how can I do that ?
    // like "theValueInv".i nnerText = "bla bla bla bla"; if the resource
    with id="place1" is for ewxample a <p>
    }


    I hope you clever guys can help this newbie with this ...
    TIA
    H




  • Lasse Reichstein Nielsen

    #2
    Re: How do I change HTML objects with my JS strings ?

    "H" <nospamplease__ henke0001@hotma il.com> writes:
    [color=blue]
    > Ok, heres the deal ;
    >
    > I have my script code like this
    >
    > var v = "place";
    > for (var i = 0 ; i < 5 ; i++)
    > {
    > v = v + i; // or v = "place" +i;[/color]

    You want 'v = "place" +i;'. What you have here will give you "place1",
    "place12", "place123", etc.
    [color=blue]
    > // Here I want to access the resource in the HTML doc, whos id="place1",
    > id="place2", etc etc, so how can I do that ?[/color]

    var elem = document.getEle mentById(v);
    [color=blue]
    > // like "theValueInv".i nnerText = "bla bla bla bla"; if the resource
    > with id="place1" is for ewxample a <p>[/color]

    You can use innerText, but that only works in IE and Opera 7. Or you
    could use DOM methods that works in all modern browsers (for IE, here
    that means IE 5+)

    // clear old content
    while(elem.hasC hildNodes()) {
    elem.removeChil d(elem.lastChil d);
    }
    // add new
    elem.appendChil d(document.crea teTextNode("bla bla bla bla"));

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Cornford

      #3
      Re: How do I change HTML objects with my JS strings ?

      "H" <nospamplease__ henke0001@hotma il.com> wrote in message
      news:i8fvb.3860 4$dP1.142394@ne wsc.telia.net.. .[color=blue]
      > var v = "place";
      > for (var i = 0 ; i < 5 ; i++)
      > {
      > v = v + i; // or v = "place" +i;
      >// Here I want to access the resource in the HTML doc, whos
      >//id="place1", id="place2", etc etc, so how can I do that ?
      >// like "theValueInv".i nnerText = "bla bla bla bla"; if the
      >//resource with id="place1" is for ewxample a <p>
      > }[/color]

      Look at:-

      <URL: http://www.jibbering.com/faq/#FAQ4_39 >

      -and the linked resource.

      But if your interest is on locating elements within a document that have
      a specific (and unique, as required) ID then the document.getEle mentById
      function is probably the best option:-

      function getEl(id){
      if(document.get ElementById){ //all DOM browsers
      return document.getEle mentById(id);
      }else if(document.all ){ //IE 4 fall-back
      return document.all[id];
      }else if(document.lay ers){ //Netscape 4
      return document.layers[id];//but that will only find positioned
      //elements on Netscape 4.
      }
      return null; //no suitable method exists on this browser.
      }

      ....
      var el, v = "place";
      for(var c = 0;c < 5;c++){
      el = getEl(v+c);
      if(el){ //so only if the returned value was non-null.
      ... //do something with the element.
      }
      }
      ....

      Richard.


      Comment

      Working...