NewUser
NewUser

Reputation: 13333

jQuery on click a div showing error

I have my markup like this. I want that when someone click on the div demo then it will redirect to the second anchor tag link.

   <div class="demo">
      <div class="social">
        <ul>
          <li>
            <a class="icon"></a>
          </li>
          <li>
            <a href="http://test.com/page" target="_blank" class="icon"></a>
          </li>
        </ul>
      </div>
    </div>

So for that I have my jquery like this

jQuery(document).ready(function() {
  jQuery(".demo").on('click', function () {
    var link = jQuery(this).find('li:last-child').children('a').attr("href");
    window.location.href = jQuery(link);
  });
});

But this one is showing error in console tab like Uncaught Error: Syntax error, unrecognized expression: with the link . So how to solve this issue?

Upvotes: 1

Views: 56

Answers (2)

Jai
Jai

Reputation: 74738

change this:

window.location.href = jQuery(link);

to this:

window.location.href = link;

That is a variable which stored a href attribute value, so you don't have to wrap it with jQuery to create an object of it.

Upvotes: 0

Adil
Adil

Reputation: 148110

Assign the href string you got in link variable instead of jQuery object, wrapping link in jQuery function consider it as variable passed selector. I think you wont have any variable defined with the name equal to value of link(varible)

Change

window.location.href = jQuery(link);

to

 window.location.href = link;

Upvotes: 1

Related Questions