Hi, need help with this very small peice of code

Discussion in 'JavaScript' started by Alexj17, Mar 10, 2009.

  1. #1
    Hi All,

    Hope you can help with this, it seems very simple but i havent quite figured out JS yet.

    Anyways, i want to be able to add a variable to what already looks like a variable (but that is not declared anywhere). This is the code...

    $("#comments").append("<tr><td>"+name+"</td><td>"+comment+"</td><td>"+json.response.dt+"</td></tr>")
    Code (markup):
    Where $("#comments") is, i want to be able to add a variable int at the end of comments so it will be like "comments88" or what ever. How is this done please?

    Alex
     
    Alexj17, Mar 10, 2009 IP
  2. Joak1m

    Joak1m Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It looks like jQuery, read from the manual about the append.
    www.jquery.com
     
    Joak1m, Mar 10, 2009 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    $("#comments").(xxx):
    xxx is the JQuery JS that will be execited on the element with the id "comments",.

    So do you mean that you want the javascript code to be executed on the element with the ID comments88
     
    nabil_kadimi, Mar 10, 2009 IP
  4. Alexj17

    Alexj17 Member

    Messages:
    173
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #4
    Nabil,

    pretty much yeah,

    im planning to use the code for many different comments so i need to change the id "comments" to allow for different instances, so which is why i want to add a variable to it. it might be comments88 or comments63 whatever variable is passed into the code.

    Thanks
     
    Alexj17, Mar 10, 2009 IP
  5. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #5
    simply replace the "#comments" string by "#commentsXX", just keep in mind that $("some_selector") is the jquery way to choose what element to execute code on (where "some_selector" is a string, a CSS/XPATH selector):

    $("div") : all divs

    $("#comment_123") : all comments that have the id comments_123 like this one
    <div id="comment_123">...</div>

    $("div .info"): all elements inside a div that have the info class

    ...

    Further reading
     
    nabil_kadimi, Mar 10, 2009 IP
  6. Alexj17

    Alexj17 Member

    Messages:
    173
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #6
    Hi

    Yes i know all this, what i cannot do and am asking help for is ....

    I already have the
    <div id="comment_1">...</div>
    <div id="comment_2">...</div>
    <div id="comment_3">...</div>

    setup

    but i cannot edit the $("#comment") to place a $("#comment_VARIABLEHERE") so the same code can be used for both 1 2 and 3.
     
    Alexj17, Mar 11, 2009 IP
  7. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Simply put it like this:

    $("#comment_"+VARIABLEHERE+"")

    That should do the trick.
     
    Ikki, Mar 11, 2009 IP
  8. Alexj17

    Alexj17 Member

    Messages:
    173
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #8

    YOU A TOTAL STAR !!! :)
     
    Alexj17, Mar 11, 2009 IP
  9. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #9
    this part of the code is completely useless:

    +""
    Code (markup):
     
    nabil_kadimi, Mar 11, 2009 IP
  10. bhagwant.banger

    bhagwant.banger Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    60
    #10
    var commentNum;
    $("#comments_"+commentNum).append("<tr><td>"+name+"</td><td>"+comment+"</td><td>"+json.response.dt+"</td></tr>")




    The above code will work.
    I feel you are trying to full out this variable name as well from the database if that is the case then you can assign the ids in the dynamic page itself and can input it here through

    this.parentNode.id

    :)
     
    bhagwant.banger, Mar 11, 2009 IP
  11. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You're right. It's completely unnecessary, but it won't hurt either so there's no problem.
     
    Ikki, Mar 11, 2009 IP
  12. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #12
    Yes you're right, maybe it have been there inadvertently after multiple modifications
     
    nabil_kadimi, Mar 12, 2009 IP