question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

V5 dropdown in table Javascript error

See original GitHub issue

Howdy! I’ve got a dropdown inside of a table that throws an error when clicked (if clicked twice, it displays the dropdown, but not on the first click). Outside of the table, it functions without error. Inside the table, it throws the error.

The Error:

Uncaught TypeError: this._menu is undefined
    show dropdown.js:142
    toggle dropdown.js:138
    _addEventListeners dropdown.js:235
    i event-handler.js:102
    j event-handler.js:208
    on event-handler.js:242
    _addEventListeners dropdown.js:233
    we dropdown.js:107
    dropdownInterface dropdown.js:388
    <anonymous> dropdown.js:519
    n event-handler.js:120
    j event-handler.js:208
    on event-handler.js:242
    <anonymous> dropdown.js:517
    <anonymous> bootstrap.bundle.min.js:6
    <anonymous> bootstrap.bundle.min.js:6

Code producing error:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

<table>
//loop
<tr><td>
  <a class="btn btn-primary actionButton" href="#" data-user='gjg0007' data-bs-toggle="dropdown">fred</a>
</td></tr>
//end loop
</table>

<div id="classMenu" class="dropdown-menu">
  <a tabindex="-1" href="#" class="dropdown-item Class1">Class 1</a>
  <a tabindex="-1" href="#" class="dropdown-item Class2">Class 2</a>
  <a tabindex="-1" href="#" class="dropdown-item Class3">Class 3</a>
</div>

<script>
$classMenu = $("#classMenu");
$(".actionButton").click(function() {
  var username = $(this).data("user");
  //move dropdown menu
  $(this).after($classMenu);
  //update links
  $classMenu.find(".Class1").attr("href", "site.php?username="+username+"&item=Class1");
  $classMenu.find(".Class2").attr("href", "site.php?username="+username+"&item=Class2");
  $classMenu.find(".Class3").attr("href", "site.php?username="+username+"&item=Class3");
  //show dropdown
  $(this).dropdown();
});
</script>

Code with no error:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

//loop
<a class="btn btn-primary actionButton" href="#" data-user='gjg0007' data-bs-toggle="dropdown">fred</a>
//end loop

<div id="classMenu" class="dropdown-menu">
  <a tabindex="-1" href="#" class="dropdown-item Class1">Class 1</a>
  <a tabindex="-1" href="#" class="dropdown-item Class2">Class 2</a>
  <a tabindex="-1" href="#" class="dropdown-item Class3">Class 3</a>
</div>

<script>
$classMenu = $("#classMenu");
$(".actionButton").click(function() {
  var username = $(this).data("user");
  //move dropdown menu
  $(this).after($classMenu);
  //update links
  $classMenu.find(".Class1").attr("href", "site.php?username="+username+"&item=Class1");
  $classMenu.find(".Class2").attr("href", "site.php?username="+username+"&item=Class2");
  $classMenu.find(".Class3").attr("href", "site.php?username="+username+"&item=Class3");
  //show dropdown
  $(this).dropdown();
});
</script>

**Updated to reflect looping

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
griffgjcommented, May 27, 2021

Perfect! Thank you!

1reaction
ffooddcommented, May 26, 2021

IMHO this is more related to accessibility than JS, for the disclosure ARIA pattern to work as intended and described.

Targetting an outside element would require an extra association on the trigger button, using aria-controls to specify which élément it acts on. I think we don’t support this for dropdowns, however that’s more or less what’s done in tabs (and maybe collapses too) .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap 5 dropdown is not working on Angular 12
I used the command npm i to install the packages without mentioning any version name so I guess the latest version was installed....
Read more >
dropdown menu in a .table-responsive · Issue #11037 - GitHub
If you have an Dropdown (e.g. from a Button) inside a table-respnsive object than the dropdown is not display correct on small devices...
Read more >
[Solved]-Bootstrap dropdown not working - Studytonight
Here we have listed out the reasons why the dropdown is not working and also added the solution to make it workable using...
Read more >
Dropdowns · Bootstrap v5.1
Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They're made interactive with the included Bootstrap dropdown JavaScript ...
Read more >
Bootstrap 5 Dropdowns - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found