1

I have the following HTML

<div class='tile'>
    <a href='http://test-site.local' >
        <div class='tileContents'>
            <div class='tileImage'>
                <i class='icon-'></i>
            </div>
            <div class='tileCaption'>
                <h4>Logos</h4>
                <p></p>
            </div>
        </div>
    </a>
</div>

<div class='tile'>
    <a href='http://test-site.local' >
        <div class='tileContents'>
            <div class='tileImage'>
                <i class='icon-'></i>
            </div>
            <div class='tileCaption'>
                <h4>Adverts</h4>
                <p></p>
            </div>
        </div>
    </a>
</div>

I would like to update the href property to any URL I wish using the values of the h4 tags as a way to target and differentiate the two DIVs as these have the same class name.

So for example I would like to update the URL in the a tag of the first div to:

<a  href="http://www.google.com">

so if the value of the h4 tag = "Logos" then update the URL to "http://www.google.com"

And the same for the second DIV:

<a href="http://www.yahoo.com">

so if the value of the h4 tag = "Adverts" then update the URL to "http://www.yahoo.com"

I have the following but I don't think I am getting the selectors correctly:

<script src="/script/jquery-1.10.2.min.js" type="text/javascript"></script>  
<script type="text/javascript">
$(document).ready(function(){  
    var value = $('div.tile h4').html();
    if (value = "Logos");
    $("DIV.tile a").attr("href", "http://www.google.com");
    if (value = "Adverts");
    $("DIV.tile a").attr("href", "http://www.yahoo.com");
});
</script>

Any help with this would be appreciated..

4 Answers 4

3

Go through every tile and change its children('a')'s href

$('.tile').each(function(){
   $(this).children('a').attr("href", "http://test-site.local/"+$(this).find('h4').text());
});

The difference between .find() and .children() is that .find() finds what you want inside the element while .children() only travels a single level down the DOM tree (so it's faster).

Use .text() to get the word and simply put it behind the url rather than using if statements (so it will be faster).

Sign up to request clarification or add additional context in comments.

Comments

2

You may try this

$('div.tile').each(function(){
    var a = $(this).find('a:first');
    a.attr('href', a.attr('href') +'/'+ $(this).find('.tileCaption h4').text());
});

DEMO

Update : After a long chat with OP I got what was the requirement and this is the solution :

var urls = {
    'Logos':'http://www.google.com/',
    'Adverts':'http://www.yahoo.com/'
};

$('div.tile').each(function(){
    var a = $(this).find('a:first');
    a.attr('href', urls[$(this).find('.tileCaption h4').text()]);
});

DEMO.

7 Comments

This worked perfectly and without using if statements. My jquery worked in updating the URLs but it was setting the same URL for all the divs. Excellent answer
Lastly what if I wanted to change the URL to something different than the value of the h4 tag? I now remember why I was using the if statement in the first place..
@DanielPerez, Here $(this).find('.tileCaption h4').text() is adding the h4 text, so you may change this part with the content you want to replace.
Thanks for this, but I think this will give the same URL to the a tag on both DIVs. What I need to do is to be able to do is to give different URL values to the a tags in the DIVs if that makes sense
@DanielPerez, Maybe i'm confused bur a.attr('href') part is adding the source url(href", "http://test-site.local) then +'/'+ adds the slash and then $(this).find('.tileCaption h4').text() adds the rest from the h4, so if it makes you clear.
|
1

First off : the correct selector syntax for a class attribute is .className, not #className (#className would search for a node who's id is "className").

Then : you should iterate through your nodes, and repeatedly call a function on each one of them. Use the .each() function :

$('.tile').each(function(){
     //when this function executes, 'this' points to the current node

     // $(this).find(...) will look only through the descendants of 'this'
     var h4 = $(this).find('h4').html();

     $(this).find('a').attr('href', "http://test-site.local/"+h4 );
});

Comments

0
$('.tile').each(function () {
    $('> a', this).attr('href', function(){
        return this.href + $(this).find('h4').text();
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.