SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inserting html with jquery

    below are two snippets of html that are repeated several time on the page.

    when the a.reply_to is clicked, id like to insert "<p>here is some text</p>" below p.posted_on a few nodes down, but only once directly after the first p.posted_on it finds further down the page


    Code:
    <span id='links'>
    <a class='reply_to' href='#'>Reply to</a>			
    </span>
    <p>tom said: </p>
    <p>bla bla bla</p>
    <p class='posted_on'>posted on 1248175097</p>
    <hr />
    
    <span id='links'>
    <a class='reply_to' href='#'>Reply to</a>			
    </span>
    <p>tom said: </p>
    <p>bla bla bla</p>
    <p class='posted_on'>posted on 1248175097</p>
    <hr />
    this jquery snippet insert it after every .posted_on and im not sure how i can make it insert only after the first match it finds? ive been playing around with child, closest etc but can not get it to work. anyone?

    Code:
    $(".reply_to").click(function () {
      var text = "<p>here is some text</p>";
      $(text).insertAfter(".posted_on");
     });
    I need someone to protect me from
    all the measures they take in order to protect me

  2. #2
    SitePoint Enthusiast aldomatic's Avatar
    Join Date
    Dec 2006
    Location
    Big D
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:

    Code:
     $(text).insertAfter(".posted_on:first");

  3. #3
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, thanks for your reply

    in fact, this inserts the string after the first .posted_on it finds starting from the top of the document. what im trying to do is insert it after the first .posted_on it finds starting from the link that was clicked

    i changed the markup so that it works with .insertAfter(this) - all good!
    I need someone to protect me from
    all the measures they take in order to protect me

  4. #4
    SitePoint Enthusiast aldomatic's Avatar
    Join Date
    Dec 2006
    Location
    Big D
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet.


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
  •