views:

68

answers:

4
+1  Q: 

Execute javascript

Hi, I have used a script I have found. Where it should do some thing when the mouse hovers over the element

    $this.hover(
        function () {
            tip.html("<p>" + tTitle + "</p>");
            setTip(tTop, tLeft);
            setTimer();
        },
        function () {
            stopTimer($this);
            tip.hide();
        }
    );

But i want to execute it, without I have to hover the mouse over the element?

How can I do that?

A: 

You can pull the code out of the function:

tip.html("<p>" + tTitle + "</p>");
setTip(tTop, tLeft);
setTimer();
Mark E
A: 

To trigger the hover on:

$this.trigger('mouseover');

To trigger the hover out:

$this.trigger('mouseout');

Another choice is to move the definition outside of the callback, and do it like this:

function onMouseOver() {
    tip.html("<p>" + tTitle + "</p>");
    setTip(tTop, tLeft);
    setTimer();
}

function onMouseOut() {
    stopTimer($this);
    tip.hide();
}

// bind the hover event

$this.hover(onMouseOver, onMouseOut);

// or use them manually:

onMouseOver();
onMouseOut();
reko_t
`.hover()` maps to `mouseneter` and `mouseleave`, rather than `mouseover` and `mouseout` :)
Nick Craver
I'm fairly sure that triggering `mouseover` and `mouseout` trigger the hover events.
reko_t
A: 

I have tried to pull the code out of the function, but that didn't help. But the problem was that i tried to call a function that hadn't been initialized yet.

Sorry for the inconvenience, and thanks:)

Alex Sleiborg
A: 

If you want it to run outside of the hover be sure to enclose the function calls in a $(document).ready or your elements wont be ready without the DOM loaded

Byron Cobb