Joshua Fleuri
Joshua Fleuri

Reputation: 57

How can I make "style.property" work on multiple elements?

I'd like to make this script work on multiple elements.

This script doesn't work if I don't add the [0] next to "tooltipSpan" in "tooltipSpan.style". But I want it to work on multiple elements.

$(document).ready(function(){
    var tooltipSpan = $('.hover-infobulle');

    if (tooltipSpan.length) {
        window.onmousemove = function (e) {
            var x = e.clientX, y = e.clientY;
            tooltipSpan.style.top = (y + 20) + 'px';
            tooltipSpan.style.left = (x + 20) + 'px';
        };
    }
});

How can I do this?

Upvotes: 0

Views: 58

Answers (2)

Quentin
Quentin

Reputation: 943571

Loop over the elements with a for statement.

… or just use the jQuery css() method since you already have a jQuery object.

To quote the documentation:

$( this ).css( "color", "red" );

Replace $( this ) with the jQuery object you already have and "color", "red" with the property and value you want to change.

Upvotes: 2

Jonas Wilms
Jonas Wilms

Reputation: 138267

There is no sense in having multiple tooltips at the exact same position. Put all the tooltips into another element, i.e. #tooltip, then move that around. Append tooltips to that element then.

Upvotes: 0

Related Questions