0

I am using selenium addon as well as jquery in my addon. Due to use of jquery functions with $ being used in selenium throwing function not found error. Removing the Jquery, everything works fine. Using Jquery (ajax call) is must for me. Please suggest how can I make them work together.

1 Answer 1

1

One recommended way to solve this kind of conflict is to wrap your javascript code inside a function, and pass jQuery as an argument to this function :

// e.g : turn this code :
$(function(){
    $('.my-class').on('click', function(){
        $.ajax(...);
    });

    ...
});

// into :
(function($) {  // start an anonymous function,
                // whose first argument is named '$' ...

    $(function(){
        $('.my-class').on('click', function(){
            $.ajax(...);
        });

        ...
    });

}(jQuery)); // and call this function right away,
            // passing the jQuery object as first argument

Note that $ is just a shortcut for jQuery :

jQuery('.my-class') and jQuery.ajax(...)
// are exactly the same as :
$('.my-class') and jQuery.ajax(...)

You can also use your own alias :

var $j = jQuery;

If some day you need to mix jQuery with another library which defines a $ variable, you can also use jQuery.noConflict() (example taken from this use case) :

var $j = jQuery.noConflict();
Sign up to request clarification or add additional context in comments.

2 Comments

Error is not being thrown while calling jquery. Error occur during selenium function calls. I tried replacing $ with jquery. It didn't work. Actually I need a way so that $ in selenium not being recognised as Jquery.
jQuery.noConflict() is the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.