Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: get link by class or selector

I'm creating a list of images from a folder, and then I open them in a fancybox... so I have thumnails, big images, and a text link to full image.

<div id="download_image">
  <p><a href="images/download/folder/big/<?php echo $file;?>" class="fancybox"><img src="<?php echo $path;?><?php echo $file?>" /></a></p>
  <p><a href="images/download/folder/full/<?php echo $file;?>.zip" class="btn-download">DOWNLOAD</a></p>
</div>

Then I'm getting images path by jquery, for each image:

var download_image = jQuery('#download_image');
var images  = download_image.find('img');

download_images.each(function(){
    var img = jQuery(this);
    var img_source = img.attr('src');

Now I need to get the url of the "btn-download" link for each image... but I don't know which selector must I use! Any advice?

like image 720
user1292580 Avatar asked Mar 26 '12 09:03

user1292580


People also ask

What is the difference between the id selector and class selector in jQuery?

Differentiate the concepts of ID selector and class selector: The only difference between them is that “id” is unique in a page and it is applied to one HTML element while “class” selector can apply to multiple HTML elements.

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How do you select an element with a particular class selected?

The .class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.


2 Answers

Try this

$('a[class=btn-download]').attr('href')
like image 109
Amir Ismail Avatar answered Oct 14 '22 01:10

Amir Ismail


Use $('.btn-download') to select an element by class.

If you want to get the href attribute then simply use:

var href = $('.btn-download').attr('href');

To loop through each element use:

$('.btn-download').each(function(){
    var href = $(this).attr('href');
    // do something with href
})
like image 24
Ben Carey Avatar answered Oct 14 '22 00:10

Ben Carey