James Dunay
James Dunay

Reputation: 2724

Simple Jquery Css style calls

Whats wrong with this?

var hover = $('<img />').attr('src', hovers[i]).css('position', 
                                                        'absolute', 
                                                        'visibility' , 
                                                        'hidden');

for some reason the 'visibility' , 'hidden' does not get called? but if i remove the position style it does.

What am i doing wrong here?

Upvotes: 0

Views: 112

Answers (3)

jeffery_the_wind
jeffery_the_wind

Reputation: 18158

http://api.jquery.com/css/

from the css docs you can see that your syntax is not correct, when define mulitple css attributes use :, then comma to separate, also enclose all the attributes in curly brackets, like this:

var hover = $('<img />').attr('src', hovers[i]).css({'position':'absolute','visibility':'hidden'});

Upvotes: 0

Geoff Warren
Geoff Warren

Reputation: 414

Change it to this:

    var hover = $('<img />').attr('src', hovers[i]).css({'position': 'absolute', 'visibility': 'hidden'});

Upvotes: 0

Prestaul
Prestaul

Reputation: 85135

The answer is to pass an object with key value pairs into the css method rather then passing extra arguments:

var hover = $('<img />')
    .attr('src', hovers[i])
    .css({
        position: 'absolute',
        visibility: 'hidden'
    });

See the documentation here for ".css(map)": http://api.jquery.com/css/#css2

Upvotes: 4

Related Questions