Results 1 to 4 of 4

Thread: change elements

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default change elements

    Hi I have a table,
    and I want to replace it with another table when I click on button, how can I do it?

    I prefer a solution that is not based on css, since I want to remove the last table from the page.

    I think there is need here for ajax, but I'm not sure.

  2. #2
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Lightbulb

    Sorry, I'm not good at CSS. But I can do it with JavaScript:

    HTML Code:
    <html>
    <head>
    <title>My Table Page</title>
    <script>
    function showhide(id){ 
    if (document.getElementById){ 
    obj = document.getElementById(id); 
    if (obj.style.display == "none"){ 
    obj.style.display = ""; 
    } else { 
    obj.style.display = "none"; 
    } 
    } 
    } 
    </script>
    </head>
    <body>
    
    <div id="div1">
    <table id="table1">
    TABLE CONTENT
    </table>
    </div>
    
    <div id="div2">
    <table id="table2">
    TABLE CONTENT
    </table>
    </div>
    
    <br><br>
    <input type="submit" value="Switch Tables!" onClick="showhide('div2'); return(false);">
    </body>
    </html>
    Is this what you mean?

  3. The Following User Says Thank You to kaos For This Useful Post:

    d-machine (08-25-2009)

  4. #3
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Hi thank you,
    but actually it's not what I'm looking for. I need something more complicated:

    I have a table with xxx id, and I want to replace it with another table with the same id.
    Actually I just want to change its td's (the new table can have less or more td's).
    All the table have only 1 tr.

  5. #4
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    im not sure if this would work on a table (i know it will work on a div, but lets give this a shot), but try assigning the HTML to some javascript variables and then use the innerHTML method like so:

    Code:
    <html>
    <head>
    <script>
    var oldContent = "<tr><td>Old Stuff</td></tr>"
    var newContent = "<tr><td>New Stuff</td><td>More Stuff</td></tr>"
    function tableChange(id, old, new){
         if (document.getElementById(id).innerHTML == old){
         document.getElementById(id).innerHTML = new;
         }
         else{
         document.getElementById(id).innerHTML = old;
         }
    }
    </script>
    </head>
    <body>
    <table id="myTable">
    <tr><td>Old Stuff</td></tr>
    </table>
    <input type="button" value="Change Table" onClick="tableChange('myTable', oldContent, newContent)">
    let me know how it goes... cheers

Bookmarks

Posting Permissions

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