Grow your CSS skills. Land your dream job.

If Div has Link jQuery?

  • # March 20, 2012 at 2:14 pm

    So I’m making this “news feed”, and I want to include a thumbnail if the div has a link, so is there any way using jQuery (or PHP if possible) to detect if the div includes a link, and if it does do something? Also, the div is classed “updates” and it’s repeated, so I should be able to do this to every div.

    # March 20, 2012 at 2:20 pm

    Like this?

    http://jsfiddle.net/KayJr/

    # March 20, 2012 at 2:28 pm

    Yes exactly, but can we use $(this) so that I can use it with multiple divs with the class of .updates? I tried, but no luck.

    # March 20, 2012 at 2:45 pm

    not sure what you mean
    you want to find anything with the class of updates with the $(this) div?

    # March 20, 2012 at 2:50 pm

    http://jsfiddle.net/3yXtk/ :)

    # March 20, 2012 at 2:56 pm

    http://jsfiddle.net/3yXtk/2/

    # March 20, 2012 at 2:58 pm

    Great, but my goal is to append something to it. Is that possible using appendTo?

    # March 20, 2012 at 3:24 pm

    http://jsfiddle.net/3yXtk/3/

    # March 20, 2012 at 4:05 pm

    Wonderfull! Can I somehow grab the href of that link as well?

    http://jsfiddle.net/3yXtk/4/

    # March 20, 2012 at 4:26 pm

    http://jsfiddle.net/3yXtk/5/

    # March 20, 2012 at 4:29 pm

    Thank you so much :)

    # March 20, 2012 at 4:42 pm

    OK, so my code is like this:

    $("div.updates").each(function() {
    if ($(this).find('a').length > 0) {
    var theHref = $(this).find('a').attr('href');
    $(this).find('a').parent('div').append("
    ");
    }
    });

    Now on my feed there is a “Full Post” link, could I exclude any links that include “/post.php” or something like that?

    # March 20, 2012 at 4:43 pm

    Or well, I realize I have tagging as well, and #search, so edit that to any link that look like @this , #this or “Full Post”?

    # March 20, 2012 at 4:47 pm

    you could do something like this

    if(theHref ==="/post.php"){
    //The link includes /post.php
    //Do something here
    }
Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".