Hi I got in a bit of a muddle here. Basically say you have this html:

<a href="javascript:void(0)">
    <img alt="" data-name="myCar" data-family="ferrari" data-type="Fast Family" src="images/ferrari.png" style="left: 193.291px; opacity: 0.5; cursor: default;">    
<a href="javascript:void(0)">
    <img alt="" data-name="yourCar" data-family="aston" data-type="Fast Family" src="images/aston.png" style="left: 293.291px; opacity: 0.5; cursor: default;">    

In my script, there is a variable$allImages = $("a img"); containing all the images and I want to select exactly the element whose data-name attribute value is myCar and find its position - mind there are many more cars in the list.
So what I have tried first is this:
var theCar = $allImages.attr('[data-name="myCar"]').css('left');
and then
var theCar = $allImages.data('name')=="myCar".css('left');
but I think the syntax is wrong; how do I select it please?

Edited by Violet_82

4 Years
Discussion Span
Last Post by Violet_82

thanks isn't there any way I can make use of that variable $allImages at all? the above line is returning undefined for whatever reason


This worked for me:

<!DOCTYPE html>
<html lang="en">
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <a href="#">
            <img src="hacker.png" data-name="myCar" />
        <a href="#">
            <img src="hacker.png" data-name="yourCar" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                $("a img[data-name='myCar']").hide();

                // this works too:
                $allImages = $("a img");
                $.each($allImages, function () { $("[data-name='yourCar']").hide() });

Edited by pritaeas


thanks guys, it did work eventually, not sure why I was getting undefined, I deleted the code and started again, it all worked thanks for your help as usual

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.