Javascript problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • meehirjha1@gmail.com

    Javascript problem

    Hello, can any body help me out..

    I have some td element which have following ids
    ctl1_lnkPrint
    ctl2_lnkPrint
    ctl3_lnkPrint
    ctl4_lnkPrint
    ctl5_lnkPrint
    ...
    ...
    ...

    and i just know the "lnkPrint", so i have to use wildcard characters...
    (something like *lnkPrint)

    I want to trap all TD elements with the above sequence id from the html
    document (using getElementbyID. ...) and replace those td element with
    blank td element...

    Can anybody give me javascript of above scenario....

    Thanks in Advance
    Meehir

  • Andrew Scott

    #2
    Re: Javascript problem

    meehirjha1@gmai l.com wrote:[color=blue]
    > Hello, can any body help me out..
    >
    > I have some td element which have following ids
    > ctl1_lnkPrint
    > ctl2_lnkPrint
    > ctl3_lnkPrint
    > ctl4_lnkPrint
    > ctl5_lnkPrint
    > ..
    > ..
    > ..
    >
    > and i just know the "lnkPrint", so i have to use wildcard characters...
    > (something like *lnkPrint)
    >
    > I want to trap all TD elements with the above sequence id from the html
    > document (using getElementbyID. ...) and replace those td element with
    > blank td element...
    >
    > Can anybody give me javascript of above scenario....
    >
    > Thanks in Advance
    > Meehir
    >[/color]


    Meehir

    Those Id's look very asp.net to me!

    It's possible to write those Id's to a javascript var from your
    codebehind. As this is a javascript group I will not go into too much
    detail. As a side note this is one of the reasons i stopped using
    asp.net.

    ----------------- C# CODE -----------------

    string cJavascript = "var aIDs = [";

    // at this point you need to cycle through your Serverside objects and
    // using their .ClientID attrib build a string that represents a
    // javascript array. i.e. "var cIDs = ['foo', 'bar'];"

    // Then add it to the page
    this.Page.Regis terClientScript Block("uniqueSe rverSideKey", "<script
    type=\"text/javascript\">" + cJavascript + "</script>");

    -------------------------------------------

    This will add the array to the page. So you could cycle through the
    array to obtain your elemnts.

    ----------------- JS CODE -----------------

    for( var i = 0; i < aIDs.length; i++ )
    {
    var eElement = document.getEle mentById(aIDs[i]);
    }

    -------------------------------------------


    Having said that it is possible to obtain it them using just javascript.

    If the containing table has an ID that you know you could use :

    var etable = document.getEle mentsById("IdOf Table");
    var aCells = etable.getEleme ntsByTagName("t d");

    This will then return ALL the cells of the table that you can apply
    logic to in order to ensure you have the correct elements and then to do
    what you want with them.

    HTH

    Andy


    Comment

    • meehirjha1@gmail.com

      #3
      Re: Javascript problem

      Hi Andy,

      I am not 100% satisfied with ur solution,
      as i have mentioned that i dont know the exact id of td element.

      I just know later part of id in advance (eg. lnkPrint of ctl2_lnkPrint)
      and i want to scan the innerhtml part of some document through the loop
      and find those td element with lnkPrint as suffix.

      As I cant use server side code, so i dont take any benefit of ur
      solution of C#.

      Actual scenario is:
      --------------------
      i am navigating from page1 to page2, in page2 i am showing some html
      section of page1 in page2 by
      lblData.innerht ml = window.document .getelementbyid (id).innerhtml

      But out of this html, i dont want to display some td element with
      lnkPrint as suffix.

      For that, i want to scan the innerhtml part of that document through a
      loop and find those td element with lnkPrint as suffix.

      Comment

      • Yann-Erwan Perio

        #4
        Re: Javascript problem

        meehirjha1@gmai l.com wrote:
        [color=blue]
        > i am navigating from page1 to page2, in page2 i am showing some html
        > section of page1 in page2 by
        > lblData.innerht ml = window.document .getelementbyid (id).innerhtml[/color]

        This looks strange, the syntax isn't correct and suggests you get your
        data from the same page and not from different documents. Are you using
        frames? If not how do you import the nodes from the first document?
        [color=blue]
        > But out of this html, i dont want to display some td element with
        > lnkPrint as suffix.
        >
        > For that, i want to scan the innerhtml part of that document through a
        > loop and find those td element with lnkPrint as suffix.[/color]

        I'm not sure to have understood your scenario, but check whether the
        following does help you - it exemplifies some DOM methods (replaceChild,
        getElementsByTa gName, createElement) and introduces a regexp to find the
        elements you're looking for.


        ---
        <style type="text/css">
        table {
        border:thin #dd8 groove;
        }
        td {
        margin:1px;
        background-color:#ffc;
        border: solid 1px #dd8;
        font-family:Garamond ;
        width:3em;
        height:3em;
        text-align:center;
        }
        td:hover {
        border: solid 1px yellow;
        }
        </style>

        <table>
        <tbody>
        <tr>
        <td id="1_lnkPrint" >Luffy</td>
        <td id="2_lnkPrint" >Zoro</td>
        </tr>
        <tr>
        <td id="3_foo">Nami </td>
        <td id="4_lnkPrint" >Sanji</td>
        </tr>
        </tbody>
        </table>

        <form action="foo">
        <input type="button" value="foo()" onclick="foo()" >
        </form>

        <script type="text/javascript">
        function foo(){
        if(
        document.create Element &&
        document.getEle mentsByTagName &&
        document.replac eChild
        ){

        var td=document.get ElementsByTagNa me("td"); // or another container
        for(var ii=td.length; ii--;) {
        if(/lnkPrint$/i.test(td[ii].id)) {
        td[ii].parentNode.rep laceChild(
        document.create Element("td"),
        td[ii]
        );
        }
        }

        }
        }
        </script>
        ---


        HTH
        Yep.

        Comment

        • drWot

          #5
          Re: Javascript problem- document.getEle mentsByIdOrNear Enough()

          If you manipulate members of a nodelist, their indexes can change to reflect
          the new order.
          Get an array of elements that fit the id "closeness" test;
          then do whatever to each of the elements in the array.


          in your example str is a regular expression: /ctl(\d+)_lnkPri nt/;
          tag is 'td';

          function nearEnough(str, tag){
          var A= document.getEle mentsByTagName( tag);
          var X= new Array();
          var L= A.length;

          for(var i= 0; i< L; ;i++){
          var who= A[i];
          var tem= who.id;
          if(tem && str.test(tem)) X.push(who) ;
          }
          return X;
          }


          Comment

          Working...