-3

I want to manipulate the DOM using jQuery. In my website, a header is being imported by a widget. I cannot edit this code directly, but a specific anchor in the header should have target _blank. When doing this, nothing happens. I get no error in my console, and nothing changes in the DOM.

I've used this jQuery code:

jQuery(document).ready(function() {
  jQuery('#iwgh2-navigation-menu-toggle-animation-wrapper a').attr('target', '_blank');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="iwgh2-navigation-menu-site" style="" href="http://binnenland.vlaanderen.be">Anchor Text</a>

What I have checked:

  • The code is being executed. When I deliberately write a syntax error, I get an error in my console.

  • I tried to change the code to use .remove() instead of adding the anchor, but nothing happened as well.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

-1

The id you use in your JS-code is not present in your HTML. Add it there and your code will work:

jQuery(document).ready(function() {
     jQuery('#iwgh2-navigation-menu-toggle-animation-wrapper a').attr('target', '_blank');
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="iwgh2-navigation-menu-toggle-animation-wrapper">
  <a class="iwgh2-navigation-menu-site" style="" href="http://binnenland.vlaanderen.be">Anchor Text</a>
</div>

Alternatively you can also just use jQuery('.iwgh2-navigation-menu-site').attr('target', '_blank'); to add the attribute to every link with the classname.

cloned
  • 6,346
  • 4
  • 26
  • 38
-1

Try using this instead: jQuery(document).ready(function() { jQuery('.iwgh2-navigation-menu-site').attr('target', '_blank'); });

I can only assume that the parent element id does not match your selector operator.