Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .css() not working

I don't know why when I use the .css() function of jQuery it returns me this:

 Uncaught TypeError: undefined is not a function

My code is this:

HTML:

<div class="photo1 sliderPhoto">d</div>
<div class="photo2 sliderPhoto">d</div>
<div class="photo3 sliderPhoto">d</div>
<div class="photo4 sliderPhoto">d</div>

CSS:

.sliderPhoto{
    position: absolute;
    top: 0px;
    left: 0px;
    background-position: center center;
    background-size: cover;
    width: 100%;
    height: 100%;
}
.photo1{
    background-image: url('../photos/ph1.jpg');
    z-index: 6;
}
.photo2{
    background-image: url('../photos/ph2.jpg');
    z-index: 6; 
    display: none;
}
.photo3{
    background-image: url('../photos/ph3.jpg');
    z-index: 6;
    display: none;
}
.photo4{
    background-image: url('../photos/ph4.jpg');
    z-index: 6;
    display: none;
}

JQuery on $(document).ready:

$photos = [$(".photo1")[0], $(".photo2")[0], $(".photo3")[0], $(".photo4")[0]];
$photos[0].css("z-index");

when I type this jQuery code it doesn't work :( but this $photos[0] returns the exact div I need.

like image 766
user2896551 Avatar asked Dec 21 '14 20:12

user2896551


People also ask

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

Can we use CSS in jQuery?

jQuery has several methods for CSS manipulation. We will look at the following methods: addClass() - Adds one or more classes to the selected elements.

How can check CSS property value in jQuery?

How can check CSS property value in jQuery? Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css(“propertyName”);

What is addClass and Removeclass in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


1 Answers

$photos[0].css("z-index");

Should be:

$($photos[0]).css("z-index");

You have to convert DOM element back into a jQuery Object to use jQuery function.

Alternatively, it may be more efficient to simply filter the element using jQuery so that no conversion is required -

$photos.first().css("z-index");

See http://api.jquery.com/first/ for more details on the .first() method

like image 161
Gaurav Kalyan Avatar answered Oct 19 '22 21:10

Gaurav Kalyan