bahamut100
bahamut100

Reputation: 1837

onMouseOver, hyperlink and rel...help me :(

Currently, I have a carousel on JavaScript which works when we click on hyperlink, like this:

<div id="direction1">
  <p>
    <a href="javascript:" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" />
    </a>
  </p>
</div>

I would like execute this event with a onMouseOver too. So I try this, but it doesn't work:

<div id="direction1">
  <p onMouseOver="this.getElementsByTagName('a').click()">
    <a href="javascript:" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" />
    </a>
  </p>
</div>

How to fix it?

PS:

This is the JS code in question:

next: function () {
        if (this.current) {
            var currentIndex = this.current._index;
            var nextIndex = (this.slides.length - 1 == currentIndex) ? (this.options.circular ? 0 : currentIndex) : currentIndex + 1;
        } else {
            var nextIndex = 1;
        }

        if (nextIndex == 0 && this.options.circular && this.options.effect != 'fade') {
            this.scroller.scrollLeft = 0;
            this.scroller.scrollTop  = 0;
            nextIndex = 1;
        }

        if (nextIndex > this.slides.length - (this.options.visibleSlides + 1)) {
            nextIndex = this.slides.length - this.options.visibleSlides;
        }       

        this.moveTo(this.slides[nextIndex]);
    }

Upvotes: 2

Views: 888

Answers (3)

Marcel Korpel
Marcel Korpel

Reputation: 21763

Where in your code is the click event assigned to the anchor element? Moreover, why do you use a separate element solely to assign a mouseover event?

You can better use

<div id="direction1">
  <a href="http://linktothepicture" onmouseover="yourObject.next()" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" /></a>
</div>

or even better assign the JavaScript in a seperate file:

window.onload = function () {
  var nextButton = document.getElementById("idOfNextButton");
  nextButton.onmouseover = yourObject.next;
};

This way, when a user doesn't have JavaScript enabled and/or middle-clicks on the next-button, your site will still function.

Upvotes: 1

Gautier Hayoun
Gautier Hayoun

Reputation: 2922

There is two problems here.

As Jeaffrey said getElementsByTagNames returns an array so your code may look like this too.

<p onMouseOver="this.getElementsByTagName('a')[0].click()">

But this won't work neither as there is no convenient way to generate a click programatically in vanilla javascript. See In JavaScript can I make a "click" event fire programmatically for a file input element?. It's still possible with at least jQuery : http://api.jquery.com/click/, but including such a dependency for this seems overkill.

The best solution I see in your situation would be to just call the a[href] method directly in the p[onmouseover].

<p onMouseOver="yourObjectInWichNextIsdefined.next()">

Upvotes: 0

Jeaf Gilbert
Jeaf Gilbert

Reputation: 11981

<div id="direction1">
    <p onMouseOver="document.getElementById('myLink').click()">
        <a id="myLink" href="javascript:" class="carousel-control" rel="next"><img class="fleche_suiv" src="/project/images/next.png" alt="Suivant" title="Suivant" />
        </a>
    </p>
</div>

'getElementsByTagName' returns an array.

Upvotes: 0

Related Questions