NoReceipt4Panda NoReceipt4Panda - 1 year ago 35
HTML Question

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="" value="Pool Party 21" pdf-data=""></option>

<img class="image_picker_image" src="">

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 = $(' 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?

Answer Source

You have to concatenate the first variable into the second selector

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