0

How can I remove On click attribute using "nav-item dropdown" class

<li class="nav-item dropdown">                    
<a class="nav-link " "="" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="window.location.href='/camping-tents'">Camping Tents</a></li>

<li class="nav-item dropdown">                    
<a class="nav-link " "="" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="window.location.href='/camping-tents2'">Camping 3</a></li>

<li class="nav-item dropdown">                    
<a class="nav-link " "="" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="window.location.href='/camping-tents3'">Camping 3</a></li>

<li class="nav-item"><a class="nav-link " bell-tents" style="background:0;"> Bell Tents</a></li>
Shawn
  • 67
  • 1
  • 1
  • 5
  • Please see [ask]. What have you tried? Does this answer your question? [How can I remove an attribute with jQuery?](https://stackoverflow.com/questions/1785071/jquery-remove-attribute) – isherwood Mar 21 '22 at 18:17
  • Your HTML has extra characters. Is that what it actually looks like? – isherwood Mar 21 '22 at 18:17

2 Answers2

1

You can use querySelectorAll to get all links under elements that have "nav-item dropdown" class and then itirate over these a elements to remove their onclick attributes:

Array.from(document.querySelectorAll('.nav-item.dropdown a')).forEach((el) => {
  el.removeAttribute('onclick')
})
Timur
  • 1,682
  • 1
  • 4
  • 11
0

First, fix your markup issues. Then, use .removeAttribute('onclick') method on the elements in question.

Ryan O'D
  • 340
  • 2
  • 10