Reputation: 1389
I can't figure out what I'm doing wrong with the below code. I tried almost everything that I can find on this forum without success. I'm trying to get the value of the "a href" inside the H3 tag.
HTML
<div class="product" style="opacity: 1;">
<a title="product" href="url-to-product.com">..................</a>
<h3><a href="url-to-product.com">Blabla</a></h3>
</div>
<div class="product" style="opacity: 1;">
<a title="product" href="other-url-to-product.com">..................</a>
<h3><a href="other-url-to-product.com">Blabla</a></h3>
</div>
I need to get the value from the href inside the h3 tag and make it a variable to use in a getJSON function. I tried things like, closest, siblings and:
$( ".opener" ).live("click", function(event) {
event.preventDefault();
$.get($(this).attr('href'), function(data, status) {
$( "#dialog" ).dialog( "open" );
**var url = $('.product h3 > a').attr('href')+'?format=json';**
$.getJSON(url, function(data) {
I always get an "undefined" error. I can't see what I'm doing wrong.
Upvotes: 0
Views: 664
Reputation: 17366
Try like this:
$('.product h3 > a').attr('href')
$(".product").each(function(){
var a_href = $(this).find('h3 > a').attr('href');
alert ("Href is: "+a_href);
});
Upvotes: 0
Reputation: 2263
You're targeting the h3-element and trying to get its 'href'-attribute. What you want to do is target the -element inside the -element and get its 'href'-attribute.
$(this).find("h3").children('a').attr('href');
Upvotes: 0
Reputation: 581
Why not using:
$('h3 a').attr('href')
There are a many ways to solve this problem, it depends on your requirments.
Upvotes: 1