CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Arrow Executing script inside a dynamicaly created table cell

    Hello gurus,

    My objective is to execute a function when a row is added to a table.
    The row addition is done dynamicaly using JS
    to do this. i dynamicaly create a table cell and embed a <script> tag inside ( as innerHTML) .
    Code:
    "<scr"+"ipt language='javascript'> alert( 'hello'); " + "</scr"+"ipt>"
    i expect to see an alert each time the cell was added.
    but nothing showed up. what could be the problem?

    btw i cannot write
    Code:
    "<script language='javascript'> alert( 'hello'); </script> "
    because the </script> tag inside the string causes conflict with the actual <script> opening tag.

    Thanks in advance
    Last edited by akgalp; September 3rd, 2008 at 02:11 AM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Executing script inside a dynamicaly created table cell

    JavaScript cannot be used to write JavaScript. Instead, when you create the cells, you simply attach the event with its function.

    Code:
    var td = document.createElement('td');
    td.onclick = function;
    tr.appendChild(td);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: Executing script inside a dynamicaly created table cell

    Thanks for the response.
    But, no there won't be any user interaction with the cell.
    I'll try to explain more.

    First, this is not a web appln, rather it would be an hta.
    i'm creating table rows dynamically (it will depend on some file content etc)
    now each row has a column that displays a timeout field ( a countdown time ), so after the timeout, the filed ( or row ) will be deleted.

    so i'm thinking of starting a timer when the row is created( each row will have its own timer - any limitation? ). and i planned to execute a script when the row is created. and this is not working as expected.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Executing script inside a dynamicaly created table cell

    You can create timers dynamically which can call a function also created dynamically.

    Code:
    var timers = [];
    ...
    timers[timers.length] = setTimeout(function() {
      // now remove the table row
    }, 10000);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Arrow Re: Executing script inside a dynamicaly created table cell

    hmm.. well that was good..
    well i tried in another method .. seems to work, but now another problem
    Code:
    function DeleteExpiredRows()
    {
        var table = document.getElementById("Study_List");
        for( i=0; i< table.rows.length; i++)
        {
            if( typeof(table.rows[i].timestamp) != 'undefined' )
            {
              var cd = new Date();
              if( cd > table.rows[i].timestamp )
              {
                   DeleteRow(table.rows[i].id);
              }
              else
              {
                var id = table.rows[i].id;
                var timeelemnt = document.getElementById("timeout_"+id);
                time = table.rows[i].timestamp - cd;
                timeelemnt.innerHTML = Math.ceil( time/1000) +"m";
              }
            }
        } 
    }
    this function is called from timer.
    and working fine. but somehow after calling the "DeleteRow(table.rows[i].id);" fucntion, the for loop exists!!. i tried using try catch, but no exception is thrown. any idea.?

    Code:
    function DeleteRow( rowid )
    {
        var indx = document.getElementById(rowid).rowIndex;
        document.getElementById("Study_List").deleteRow( indx );
        document.getElementById("Study_List").deleteRow( indx );
        document.getElementById("Study_List").deleteRow( indx );
     }
    thanks for the reply

  6. #6
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: Executing script inside a dynamicaly created table cell

    no comments.??

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured