-1

I am using the following jQuery code in the search bar of my website. Since I am a beginner, I am unable to relate to other code examples, hence I need help in fixing the code.

jQuery(document).ready(function ($) {
    //Open Link in Search Results in New Window
    jQuery('div.search_result_item_content').click(function () {
        console.log("I am executed");
        var menuLink = $('div.col.col-9.search_result_item_content').data('link');
        window.open(menuLink, "_blank");
    });
});

The code without .ready() seems to work on the console. But not on the index.js file of my website. The same file has many other jQuery functions, which are all working smoothly.

Any idea, what is causing the issue?

Nirmal Kumar
  • 139
  • 2
  • 7

1 Answers1

0

You are generating the items dynamically so this will not work. What you can do is what I will do below.

 jQuery(document).ready(function ($) {   
    jQuery(document).on('click','div.search_result_item_content',function () {
    console.log("I am executed");
    var menuLink = $(this).data('link');
    window.open(menuLink, "_blank");
 });

That should work for you.