samedi 14 juillet 2018

Comparing jQuery(this) in a JavaScript if condition

I would like the current selector to be compared against a stored node in a JavaScript if condition.

In my code I have created an array of image nodes and li nodes. I would like to be able to compare the li that is being currently hovered over by the user to the elements in the links array, e.g., is the currently hovered link the same as links[0]?

I have tried many ways to check equality between jQuery(this) and the links, but I cannot get an if statement to find them equal. Can you tell me how to compare the current selector to selector locations stored in an array?

Below is my code (I am using jQuery for my selector because this is a Wordpress website):

jQuery("li.menu-cloud").hover( function(){

    // Identify the containers of the links and the featured images:
    var nav = document.getElementById("nav");
    var holder = document.getElementById("img-holder");

    // Creates an array of the individual links and featured images of the menu links:
    var pics = holder.getElementsByTagName("img"); 
    var links = nav.getElementsByTagName("li");

    //Store current li in a variable to test:
    var element = jQuery(this);
    if ( links[0] == element )
    {        
        alert("links[0] equals this element");

    }
    else {}


},
function() {
    alert("You have left a menucloud link.");
});

Aucun commentaire:

Enregistrer un commentaire