Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4.2.1 - Failed to execute 'querySelector' on 'Document': 'javascript:void(0);' is not a valid selector

I am working on a project which I started building on bootstrap 4.0.0. During this project, I had updated bootstrap regulary whenever the new versions ( 4.1.0 , 4.1.3 ) of bootstrap came and all the things were working perfectly until I have update to bootstrap 4.2.1 from there I encountered error in console when ever I click on the document

Uncaught DOMException: Failed to execute 'querySelector' on 'Document': 'javascript:void(0);' is not a valid selector.

I figured out that it occur due to the dropdown because of this error the dropdown does not work. I also checked, that if I am using href="javascript:void(0);", href="#!" the error occur but if i use anchor tag without href or href="#" then it is working fine.

Note:- I need the solution with href="javascript:void(0); as href="# in the address link does not look pretty and page scrolls up to the top

<div class="dropdown">
        <a class="btn btn-secondary dropdown-toggle" href="javascript:void();" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown link
        </a>

        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
            <a class="dropdown-item" href="#">Action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <a class="dropdown-item" href="#">Something else here</a>
        </div>
    </div>

Here is my codepen

like image 847
Owaiz Yusufi Avatar asked Dec 25 '18 11:12

Owaiz Yusufi


3 Answers

=== Change in bootstrap.js v4.2.1 ===

Find below block

getSelectorFromElement: function getSelectorFromElement(element) {
  var selector = element.getAttribute('data-target');

  if (!selector || selector === '#') {
    var hrefAttr = element.getAttribute('href');
    selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
  }

  return selector && document.querySelector(selector) ? selector : null;
},

Need to add try and catch exception only as below

Replace

return selector && document.querySelector(selector) ? selector : null;

with

try {
    return selector && document.querySelector(selector) ? selector : null;
} catch (err) {
    return null;
}

=== Change in bootstrap.min.js v4.2.1 ===

Replace

return e&&document.querySelector(e)?e:null

with

try{return e&&document.querySelector(e)?e:null}catch(err){return null;}
like image 143
Hardik Kondhiya Avatar answered Oct 19 '22 03:10

Hardik Kondhiya


I was having this issue, and a comment on this reported issue clued me into the solution. I was copying the example from the Bootstrap docs, and I had to remove the ID from the parent link, and instead have that ID on the container for the child links, without the aria-labeledby property, and add a reference in the parent link data-target property.

This example shows what I was doing, which caused the console errors:

<li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="/Summary/Commercial" data-target="#" data-toggle="dropdown" id="navToggleCommercial" role="button" aria-haspopup="true" aria-expanded="false">
        Commercial
    </a>
    <div class="dropdown-menu" aria-labelledby="navToggleCommercial">
        <a class="dropdown-item" href="/Summary/Dashboard">Dashboard</a>
    </div>
</li>

This is the solution that worked for me:

<li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="/Summary/Commercial" data-target="#navToggleCommercial" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
        Individual
    </a>
    <div class="dropdown-menu" id="navToggleCommercial">
        <a class="dropdown-item" href="/Summary/Dashboard">Dashboard</a>
    </div>
</li>
like image 38
Lucas Fowler Avatar answered Oct 19 '22 03:10

Lucas Fowler


Here is the solution that works for me

Snippet from Bootstrap 4.1.3

getSelectorFromElement: function getSelectorFromElement(element) {
    var selector = element.getAttribute('data-target');

    if (!selector || selector === '#') {
      selector = element.getAttribute('href') || '';
    }

    try {
      return document.querySelector(selector) ? selector : null;
    } catch (err) {
      return null;
    }
},

Replace it from Bootstrap 4.2.1

getSelectorFromElement: function getSelectorFromElement(element) {
  var selector = element.getAttribute('data-target');

  if (!selector || selector === '#') {
    var hrefAttr = element.getAttribute('href');
    selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
  }

  return selector && document.querySelector(selector) ? selector : null;
},

Thanks to PixemWeb github solution

For more info here is the link https://github.com/twbs/bootstrap/issues/27903#issuecomment-449600715

like image 2
Owaiz Yusufi Avatar answered Oct 19 '22 04:10

Owaiz Yusufi