mark
mark

Reputation: 417

How to get a CSS property of an HTML element from its class?

I am using javascript to get the src element of an img with this script:

document.addEventListener("click", function(evt) {
            var src = evt.target.src 
}

but some images don't have src attribute as it is set in the currentSrc property of its css class like this one:

<img alt="Chaussure de football enfant terrain sec Agility 900 FG bleu rouge" class="source-medium">

and the source-medium css class contains:

currentSrc: "https://contents.mediadecathlon.com/p1353468/k$6b31ed91fa5b73e99f89ff262c0985c7/s

My question is how to get the currentSrc property with javascript?

Upvotes: 1

Views: 77

Answers (1)

jake2389
jake2389

Reputation: 1174

Did you try evt.target.currentSrc?

If you only want the sources from the img elements with the source-medium class you can select those elements and get their source: document.querySelectorAll('.source-medium') and then loop over the result with a for loop or something.

Upvotes: 2

Related Questions