1

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?

NewUser
  • 12,713
  • 39
  • 142
  • 236

2 Answers2

1

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;
Adil
  • 146,340
  • 25
  • 209
  • 204
0

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.

Jai
  • 74,255
  • 12
  • 74
  • 103