Issue with implementing Bootstrap CSS class on dynamic list items

My horizontal menu contains dropdowns with li elements that are commented out dynamically. To add these dynamic li's within the dropdown-menu, I decided to use a div and append the li's using jQuery.

HTML

<li class="dropdown">
 <ul class="dropdown-menu" role="menu">
    <%-- <li ><a href="Link1">Link1</a></li>
         <li ><a href="Link2">Link2</a></li>
    --%>
    <div id="dvLinks"></div>
   </ul> 
 </li>

jQuery

var invDiv = '';
$.each(result, function (i, value) {
   invDiv += '<li><a href=' + result.href + '>' + Links + '</a></li>'; 
});

$('#dvLinks').append(invDiv);

The problem arises because the class "dropdown-menu" does not apply to the dynamic li's. Due to technical restrictions, <div> cannot be placed directly inside <ul>. I attempted another approach as shown below, but it did not yield any results on the UI. Please assist in identifying where I may have made an error.

<li class="dropdown"> 
    <div id="dvLinks"></div> 
  </li>

var invDiv = '<ul class="dropdown-menu" role="menu">';

$.each(result, function (i, value) {
   invDiv += '<li><a href=' + result.href + '>' + Links + '</a></li>'; 
});

invDiv += '</ul>'

$('#dvLinks').append(invDiv);

Answer №1

To add the LI string to the existing UL element, there is no need for a div container:

<li class="dropdown">
    <ul class="dropdown-menu" role="menu" id="dvLinks"></ul> 
</li>

The JavaScript code remains unchanged, just ensure that $('#dvLinks') now targets the UL element:

var itemList = '';
$.each(result, function (i, value) {
   itemList += '<li><a href=' + result.href + '>' + Links + '</a></li>'; 
});

$('#dvLinks').append(itemList);

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

The form submission button fails to function when the onsubmit function returns false

When submitting, I check two fields. If one of the two fields is checked, then I return true; otherwise, I return false. The issue I'm facing is that even when I check one of the checkboxes, the submit button does not seem to work (I use console.log() ...

The issue of routes malfunctioning when utilizing jQuery AJAX calls within an MVC framework

Having an issue with custom routes.maps in MVC. When I click on the login button, it shows an error message displayed below: Server Error in '/' Application The resource cannot be found. Description: HTTP 404. The requested URL was not fou ...

What is the best way to ensure that the search box automatically adjusts its position and size regardless of the screen resolution?

I'm currently working on a responsive website project and facing an issue with the absolute positioning of the search bar on the main page, which is crucial for me. Below is the code snippet: <div class="span4"> <form class="well form ...

Is there a way for me to prevent the need to open a new window to view the results?

Can someone help me with my code? I want to display results without opening a new window or replacing the current content in the same window. I'm thinking of using AJAX to show results in a dialog window using JQueryUI but I'm not sure how to do ...

Material UI Card shadow effect getting cropped

Currently experiencing an uncommon issue while using Material UI's Card component - the box-shadow is cut off on the top and bottom sides. Any suggestions on how to resolve this problem? Check out my code below: import React, { Component } from & ...

Tracking changes to variables made by jQuery within a component

I understand that this approach may not be ideal, but I have regular JavaScript code that is changing a variable within my component. The reason for this is due to dynamic JavaScript (generated from Google Blockly) being executed (through `eval`) within m ...

Difficulties with choosing predecessors

Is there a way in the HTML snippet below to extract the checkall from the thing? .. <input type="checkbox" class="checkall"><label>Check all</label> <ul> <li><input type="checkbox" class="thing"><label>Thing 1 ...

Utilizing Jquery within Reactjs

Currently, I am encountering an issue while attempting to implement Materialize CSS in React JS. Some of the CSS components require jQuery for initialization or animation. Is it advisable to use jQuery in React JS? And if so, how can I incorporate jQuery ...

Scroll through a particular HTML element using jQuery animation

Can an object be animated from one position to another following a specific path? For instance, if moving from pos1 to pos2, and encountering a div with the class "no-ok" in the way, the object should avoid it and find the nearest div named OK either to th ...

Can someone assist me in figuring out how to solve selecting multiple radio buttons at once

<script type="text/javascript"> let x = "1.html"; let y = "2.html"; function redirectPage(form){ for(let i=0; i<form.length; i++) { if(form.answerq[i].checked && form.answerw[i].checked && f ...

How can you prevent JQuery AJAX from automatically redirecting after a successful form submission?

Encountered an issue with loading http://example.com/signup.ashx: The redirect from 'http://example.com/signup.ashx' to '' was blocked by the CORS policy. This is due to the absence of 'Access-Control-Allow-Origin' header on t ...

Achieving text alignment with icons in Angular

I'm having trouble aligning my text with the icon next to it despite trying to adjust margins. I would really appreciate any help or suggestions on how to resolve this issue. <div class="notification" [ngClass]="{'no ...

Combine two JSON objects which share a common attribute

I am currently working with two JSON feeds. One feed contains the basic information about a course, while the other contains more administrative details. Here is an example to illustrate what I mean. FIRST {"courses": {"course":{"id":"4","title":"Usi ...

Modifying CSS in JQuery does not seem to have a lasting effect

My webpage uses JQuery to dynamically hide and show different parts of the content when a button is clicked. However, I am facing an issue where the changes only last for a brief moment before reverting back to the default styling... Here is a snippet of ...

I haven't quite reached success yet, but I am working on getting my slideshow to display on my local server

My homepage is fully loading, but the slideshow feature is not functioning correctly. I have a file called slide.js in my /lib directory that I am trying to integrate into my homepage. The images are referenced properly and working, but they are not displa ...

Unable to transfer information from controller to view using ajax

Having trouble with ajax in my asp.net mvc project. I am trying to pass data from the controller to the view but encountering an error: "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" I can save to the server ...

Struggling to place image behind Bootstrap navbar due to z-index issues

This layout features a navigation bar: <header id="navigation-bar" class="nav-header"> <nav class="navbar navbar-expand-lg"> <a class="navbar-brand" href="#"> <img src="images/logo.svg" alt="logo-icon"> ...

Instructions for creating a dropdown menu button that can also act as a link button

I'm struggling with my HTML code and can't seem to get the button to work as a link. Even after inserting an anchor tag with a link inside, clicking on the button does nothing. <!DOCTYPE html> <html lang="en"> <head> ...

Challenges arise when implementing bootstrap with a default font size of 10px as rem

In my current project, I've decided to integrate Bootstrap 4 and start using the 'rem' unit for lengths. Initially, I set 'rem = 10px' to simplify conversions from pixels. However, this caused a problem because Bootstrap now primar ...

File management structure designed for a particular web platform

Currently, I am utilizing Laravel 4 with Apache to create a web application and I am in need of some technical guidance on how to design a specific section of my application. This is the problem context for the area: Within my web application, there is a ...