Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

javascript Table Updating

  • 23-06-2009 11:52AM
    #1
    Registered Users, Registered Users 2 Posts: 500 ✭✭✭


    I have a table in html with a lot of rows. When something happens - I change the data in those rows with javascript.

    However this involves code like so in my javascript

    output += "<tr>"
    output += "<td>"+this.fields.firstname+"</td>"
    output += "<td>"+this.fields.surname+"</td>"
    output += "<td><a href=\"browse/"+this.fields.test+"</a></td>
    output += "<td><img src='/images/xxx.gif'/>"+this.fields.phone+"</td>"
    output += "</tr>"

    However, I dont want to hardcode any html like <"tr"> in my javascript - how can i do it?

    Please help


Comments

  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    You can add rows to a table using the DOM, rather than hard-coding any tags.

    Check this out:
    http://www.mredkj.com/tutorials/tableaddrow.html


  • Registered Users, Registered Users 2 Posts: 500 ✭✭✭warrenaldo


    Thanks Eoin,

    Didnt think I would ever get a reply.
    I have been working on it since I posted and came up with something using Jquery.

    var row = $("#tableid tr:last").clone()
    $("td:eq(0)", row).text(this.fields.firstname);
    $("td:eq(1)", row).text(this.fields.surname);

    It works well.


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Ah cool - seems to be pretty tidy code.


Advertisement