1

I have the following html example

<div id="main"><input type="hidden" name="bilbo" value="0"><a id="baggins" href="somewhere.com"></div>
<div id="main"><a id="baggins" href="somewhere-else.com"></div>
<div id="main"><a id="baggins" href="somewhere-elseagain.com"></div>

What I need to do is loop through each div with the id of main and find if the input type hidden exists.

Then I need to change the href of that link in the same div as the input hidden, each time, there will be more than one.

Any help would be appreciated.

4
  • 4
    Just a sidenote, an id has to be unique, use classes instead. Commented Apr 26, 2013 at 13:30
  • the html is a rendered rss feed...unfortunately Commented Apr 26, 2013 at 13:32
  • since this is a rendered rss feed, it is being transformed with xslt, so each of the divs is built in a for-each... Commented Apr 26, 2013 at 13:33
  • since they are built in a for each the if($('input[name=bilbo]')) doesn't work, because it only reads the first one and not the subsequent divs after that Commented Apr 26, 2013 at 13:35

2 Answers 2

2

Since you have no control over the HTML, forget the ID then.

$('div:has(input[type=hidden]) a')

This means, give me all hyperlinks that are nested inside a div element that must have a hidden input nested in the DIVs.

You should have roughly the following

$('div:has(input[type=hidden]) a').each(function (i, link) {
    $(link).attr('href', 'whatever.bla');
});

In case you are fetching the rss feed html into a string, then you can probably do this:

// assuming you fetched the rss feed as a string or responseText
var htmlString = '<div id="main"><input type="hidden" name="bilbo" value="0"><a id="baggins" href="somewhere.com"></div><div id="main"><a id="baggins" href="somewhere-else.com"></div><div id="main"><a id="baggins" href="somewhere-elseagain.com"></div>';

var $feeds = $(htmlString);

$feeds.find('div:has(input[type=hidden]) a').each(function (i, link) {
    $(link).attr('href', 'whatever.bla');
});
Sign up to request clarification or add additional context in comments.

4 Comments

What if I wanted to loop through and see which divs did NOT contain the input hiddens?
nevermind, I found that one. $('div:not(:has(input[type=hidden])) a')
$('div:not(:has(input[type=hidden])) a')
Right, you gotta love jQuery just for this
2

Assuming you use classes and not repeating IDs

$(".main").each(function() {
    var isHidden = $(this).children("input[type='hidden']").length > 0;
    if (isHidden) {
        $(this).children("input[type='hidden']").nextAll("a").each(function() {
            //Change the href now
            $(this).attr("href", "newlink");
        });
    }
});

Demo: http://jsfiddle.net/7Rc9X/

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.