Issues with dropdown checklist functionality

I'm having trouble creating a dropdown checkbox list for my blog. Every time I try to check one of the boxes, the list closes unexpectedly. Any assistance would be greatly appreciated! The element is essentially a styled dropdown with checkboxes embedded in it. It should open when the anchor is clicked or something outside the tags is clicked.

HTML

<input type="text" value="" name="ne" id="sign-up"/>
<div id="list1" class="dropdown-check-list" tabindex="100">
  <span class="anchor">Tags</span>
  <ul class="items">
    <li><input type="checkbox" name="nl1" value="1"checked ></li>
    <li><input type="checkbox" name="nl2" value="2" checked ></li>
    <li><input type="checkbox" name="nl3" value="3" checked ></li>
    <li><input type="checkbox" name="nl4" value="4" checked ></li>
    <li><input type="checkbox" name="nl4" value="5" checked >Projects</li>
    <li><input type="checkbox" name="nl5" value="6" checked ></li>
    <li><input type="checkbox" name="nl6" value="7" checked ></li>
  </ul>
</div>      
<input type="submit" id="signup-submit" value="sign up" />

CSS

.dropdown-check-list {
  display: inline-block;
  color:#49494b;
  background-color:#f0efeb;
  margin-left: -5px;
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
}

.dropdown-check-list .anchor {
  position: relative;
  cursor: pointer;
  display: inline-block;
  padding-top: 6px;
  padding-bottom: 9px;
  padding-right: 10px;
  padding-left: 5px;
}

.dropdown-check-list .anchor:after {
  position: absolute;
  border-left: 2px solid black;
  border-top: 2px solid black;
  padding: 5px;
  right: 10px;
  top: 20%;
}
.dropdown-check-list .anchor:active:after {
  right: 8px;
  top: 21%;
}
.dropdown-check-list ul.items {
  padding: 2px;
  display: none;
  margin: 0;
  border: 1px solid #ccc;
  border-top: none;
  position: absolute;
  background-color:#fff;
}
.dropdown-check-list ul.items li {
  list-style: none;
} 
.dropdown-check-list.visible .anchor {
  color: #000;
}
.dropdown-check-list.visible .items {
  display: block;
}

Jquery

var checkList = document.getElementById('list1');
checkList.getElementsByClassName('anchor')[0].onclick = function (evt) {
  if (checkList.classList.contains('visible'))
    checkList.classList.remove('visible');
  else
    checkList.classList.add('visible');
  }

  checkList.onblur = function(evt) {
    checkList.classList.remove('visible');
  }
}

Answer №1

When the onblur function is triggered, it removes the visible class causing it to disappear. To solve this issue, it is suggested to delete the code for the onblur function and attempt again.

// Code to be removed:
checkList.onblur = function(evt) {
    checkList.classList.remove('visible');
}

Alternatively, you can also try commenting out the code and testing it once more.

Answer №2

HTML and JQUERY Code:

<br/>
<div class="container">
<div class="row">
   <div class="col-lg-12">
 <div class="button-group">
    <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-cog"></span> <span  class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" class="small" data-value="option1" tabIndex="-1"><input type="checkbox"/>&nbsp;Option 1</a></li>
<li><a href="#" class="small" data-value="option2" tabIndex="-1"><input type="checkbox"/>&nbsp;Option 2</a></li>
<li><a href="#" class="small" data-value="option3" tabIndex="-1"><input type="checkbox"/>&nbsp;Option 3</a></li>
<li><a href="#" class="small" data-value="option4" tabIndex="-1"><input type="checkbox"/>&nbsp;Option 4</a></li>
<li><a href="#" class="small" data-value="option5" tabIndex="-1"><input type="checkbox"/>&nbsp;Option 5</a></li>
<li><a href="#" class="small" data-value="option6" tabIndex="-1"><input type="checkbox"/>&nbsp;Option 6</a></li>
</ul>
</div>
 </div>
  </div>
     </div>

Note : Ensure to include the Bootstrap style and script on your webpage. You can find an example on jsfiddle here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resolving Issues with Page Feature Images on Your WordPress Site

Currently enrolled in a web development course, I collaborated with my instructor to create a custom website. However, I am facing an issue where the header image appears shorter on the Resources page () and Contact page () compared to the Blog page (). De ...

Tidy provides HTML that does not conform to standard formatting

When using Tidy to clean and format HTML files to make them compliant with HTML/XHTML standards, I have noticed that the output sometimes contains non-standard attribute values, such as: <table id='abc'>... or <input type='button ...

Show a single line of data from the database and proceed to the following column

When I look at the content of each column in my database table (id, book, cover, date, bookname...), I found a method that works well for displaying the row content. <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = &apos ...

Designing exquisite button navigation

Hey everyone, I'm currently working on creating a circular button navigation for my website. I want to have 4 buttons, each with a different color, and I want them to glow when the user hovers over them. So far, this is what I have in my HTML: HTML: ...

Can you create a dropdown menu using information from one JSON file and have the selected option come from another source?

Recently, I came across this JavaScript code that is supposed to create a dropdown list of statuses: $.getJSON('/statuses', { ajax: 'true' }, function (data) { var html; var len = data.length; html + ...

Sending an array using the POST method with Ajax

It's puzzling, this seemingly straightforward issue has me stumped. I can't seem to figure out why it's happening, and I haven't come across anyone else experiencing the same problem. Here's the dilemma: I'm sending a POST re ...

What is the best way to utilize multiple Ajax requests in order to generate server-side response data?

There are two separate Ajax queries that retrieve different parts of the required server-side data for DataTables. The first query retrieves 40 rows of records as a JSON string from a table, while the second query simply returns the total count for the tab ...

jQuery Extends the loop with the picture

I currently have an animation that goes from the bottom to the top but I want it to loop smoothly without abruptly starting over. Is there any way to achieve this? JavaScript Code var ticker = $('#ticker'); var container = $('#ticker > ...

What is the best way to show a check mark when selecting a checkbox?

I have customized my checkbox to appear as a circle, and now I want to display a check mark when the user selects the checkbox. When I remove -webkit-appearance: none;, the check mark appears but my CSS stops working. Is there a way to achieve this witho ...

Having trouble uploading files with jQuery File Upload functionality

Issue: I need to set a session variable and redirect the user to a different PHP page after uploading a .txt file using jQuery File Upload. HTML code (upload.php): <!-- The fileinput-button span is used to style the file input field as button --> ...

Replace the CSS styling with new code from different files

After downloading a large web template containing numerous CSS and JS files, I have successfully created a visually appealing website. However, there are still some minor details that I wish to modify, such as the font style and colors. The issue arises w ...

Dealing with an asterisk * in an id selector: what you need to know

My PHP code generates a series of buttons for users to click on, each button representing the grade of a student. Additionally, the PHP code also creates the jQuery necessary to enable a random selection of students based on the grade clicked: echo "<d ...

NameError: The 'find_all' attribute cannot be found in the object of type 'NavigableString' (AttributeError)

import requests from bs4 import BeautifulSoup url=("http://finance.naver.com/news/mainnews.nhn") r=requests.get(url) soup=BeautifulSoup(r.content) a_data = soup.find_all("li",{"class":"block1"}) for item in a_data: print item.contents[0].find_all("d ...

Ways to release the binding of an element and then re-enable it for future use

Encountering an issue with dynamically unbinding and binding elements using jQuery. Currently working on creating a mobile tabbing system where users can navigate using left and right arrows to move content within ul.tube. The right arrow shifts the ul.tu ...

Attention: The DataTables system has issued a warning stating that there is an Ajax error with table id=dataTables -

I am attempting to retrieve data from a MySQL database using PHP and Ajax to showcase in a table utilizing DataTables. My development environment is XAMPP 1.8.3. Below is a snippet of my HTML code: <table id="dataTables-melate" class="table table-stri ...

What is the best way to place <a> links next to each other in the header?

Looking to align the links horizontally in the header using CSS, can someone assist me with this? <body> <nav> <div class="wrapper"> <a href="index.php"><img src="spaceship.png" alt="blogs logo& ...

Can you explain the discrepancy between these two CSS statements?

Comparing two CSS combinations: .firstexpression.secondexpression (no space in between) versus .firstexpression .secondexpression (with a space in between) Could you explain the distinction? ...

JQuery Form Wizard - Adding a Finish Button Linked to a Page

Having some difficulty linking the finish button on my Jquery Form Wizard within my Flask App to a link. I attempted to test the functionality by creating a simple alert, but that didn't work either. I'm certain that I missed a step somewhere, b ...

Display each table within a modal that is triggered by a separate table loop

I have a modal that includes a table. The modal is loaded from a looped table, and despite my confident code, the display seems off. When I run the code, this odd result occurs. Click the link to view the image. <tbody> <?php ...

Assistance requested in structuring JSON data

I'm currently working on making my javascript code more dynamic. Here's what I have so far: data.addRows(2); data.setValue(0, 0, 'name goes here'); data.setValue(0, 1, value 1 goes here); data.setValue(0, 2, value 2 goes here); data. ...