LatentDenis
LatentDenis

Reputation: 2991

How to find an element attribute through searching for another attribute of that element

I'm trying to access an element's attribute value, by finding a different attribute, by name, of that same element.

Here's some code:

<option data-img-src="http://example.com/pic.jpg" value="Pool Party 21" pdf-data="http://example.com/myPDF.pdf"></option>

<img class="image_picker_image" src="http://example.com/pic.jpg">

Here's some PHP where I try to get the SRC of that image, store as a variable. Then, try to find another element where the SRC of the image (the previously stored variable) is an attribute called data-img-src that holds that value.

Finally, I try to store that found-elements attribute called "pdf-data" as a string in another variable.

pdfbgImage = $('.tab-pane.active div.thumbnail.selected').children('img').attr('src');
pdfURL = $("[data-img-src=pdfbgImage]").attr('pdf-data');

I don't believe my code is right. Can anyone help?

Upvotes: 0

Views: 95

Answers (1)

adeneo
adeneo

Reputation: 318162

You have to concatenate the first variable into the second selector

var pdfbgImage = $('.tab-pane.active div.thumbnail.selected').children('img').attr('src');
var pdfURL     = $("[data-img-src='"+pdfbgImage+"']").attr('pdf-data');

Upvotes: 2

Related Questions