Implementing a dynamic loading feature using jQuery to display both menu and submenu items

Piece of Code

$(document).ready(function() {
    $("#ulSub").append('<li id="abc hover" class="DJ hover"><h5>Cancer information</h5><ul id="ulSub1 fadeIn/fadeOut">');
    var ciid = $("#ulSub li").find("ul");
    $(ciid).append('<li><h5>What is cancer</h5></li></ul></li>');
});

HTML Element

<div class="dmenu">
    <ul id="ulSub">
    </ul>
</div>

Answer №1

To accomplish this task, utilize the appropriate functions of jQuery such as .mouseover() and .mouseout().

See a functional and user-friendly code example below:

$(document).ready(function() {
  $("#ulSub").append('<li id="abc hover" class="DJ hover"><h5>Cancer information</h5><ul id="ulSub1 fadeIn/fadeOut">');
  var ciid = $("#ulSub li").find("ul");
  $(ciid).append('<li><h5>What is cancer</h5></li></ul></li>');

  if($("li").hasClass("DJ"))
    $("li").removeClass("DJ");

  $("li").mouseover(function(e) {
    $(this).addClass("DJ");
    e.stopPropagation();
  }).mouseout(function(e) {
    $(this).removeClass("DJ");
  });

});
.DJ{
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="dmenu">
  <ul id="ulSub">
  </ul>
</div>


Explore further:

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

Designing a unique diagonal layout with HTML and CSS

Hey there, I have a question about creating diagonal backgrounds in web design. I've seen websites like Udacity and one my friend is working on that feature diagonal shapes or designs in the background. I'm curious about how to achieve this effec ...

Passing data from a jQuery AJAX request to a success callback function

I am currently in the process of learning Ajax with jQuery, but there is a certain aspect that puzzles me. In the code snippet below, it can be observed that when we invoke the onSuccess function, it does not require any arguments, yet the function itsel ...

Monitoring the modifications of a linked component in React: A guide

I am facing an issue where I need to store the text of a referenced element in an array. My goal is to first display the text of each paragraph element with the "ebookName" class in the console before storing it in the array. However, I have encountered a ...

The server cannot process the content type 'application/json;charset=UTF-8' as it is not supported

As a newcomer to the Spring World, I am venturing into my first Jquery journey. I am attempting to retrieve JSON data from a JSP page using an Ajax call to the controller. Unfortunately, I encountered an error: org.springframework.web.HttpMediaTypeNotSuppo ...

Dispatching correspondence with attached documents and photographs

Is there a way to send an email with an HTML file chosen by the user using (Fileupload), which contains images that I want to embed in the body? I am looking for a method to directly embed images in the body of the email. ...

Is it possible to gradually open or close a div element?

Looking for a way to add an effect to the code below that opens/closes a div on mouseover above an image. Any examples or suggestions? I'm not exactly a javascript expert. HTML: <div> <div class="under"><img src="http://oi60.tinypic.c ...

jQuery UI causing issue with element position after drop operation

Trying to implement a drag and drop feature using jQuery UI for two HTML div elements containing canvas elements. The goal is to move canvases from one div to another. Encountering an issue where the dropped canvas does not end up in the expected position ...

Having trouble getting the Bootstrap 5 Modal to function properly within an Electron app

Facing an issue with my web app using Bootstrap 5, as the modal is not displaying properly. Below is the HTML code for the modal and the button that triggers it: <div class="modal" tabindex="-1" id=&quo ...

String object causing jQuery selector to malfunction

const newHTML = '<html><head><title>Hello</title><link rel="apple-icon-touch-precomposed" href="image.jpg" /></head><body></body></html>'; alert($(newHTML).find('link[rel="apple-icon-touc ...

Dart and external CSS and JS libraries and tools

As I prepare to dive into developing my very first web application, one technology that has caught my eye is Google Dart. The idea of using a new, innovative approach to web development excites me, and I am seriously considering utilizing it for my project ...

Are the form fields and database table fields sharing identical names?

Do you think it is a poor practice to use the same name for HTML form fields as for table field names? I'm creating dynamic SQL insert queries and currently, I am using regular expressions to change the names to match the corresponding database fields ...

What is preventing HTML from triggering JavaScript when loaded inside a <div> with a script?

I'm working on creating a collapsible menu that I can easily customize on any page without the use of iframes. As someone new to web design, I have knowledge of CSS and HTML but I am currently learning JavaScript with limited experience in jQuery or A ...

The disappearance of the final element in an array after adding a new one using JavaScript

One of the challenges I'm facing in my backbone project involves creating an Add To Cart feature using window.localStorage. Below is my javascript code for the addToCart() function: var cartLS = window.localStorage.getItem("Cart"); var cartObject = ...

The footer of the website remains separate from the overlay of the modal Popup Form

I recently implemented a modal popup form on my website that covers the entire viewport with its background. However, I am facing an issue where the footer of the website is still displayed on top of the modal background, even though everything else seems ...

Using Angular's ngFor directive to render 3 buttons in each row

I am attempting to show 3 buttons per div with the class "row". Using *ngFor to loop through the array to display buttons with the correct text. Here is a sample of my data: [{"NODE_ID":21.0,"NODE_DESC":"TERMINAL ASSEMBLY",&q ...

Can you explain the distinction between div and span tags?

HTML includes two main tags, div and span. I am curious about the specific functions and roles of each tag. I have experimented with both but haven't been able to identify any significant differences. Can someone provide a detailed explanation on the ...

Unforeseen behavior of $.Ajax within a loop

While executing my code <div id="#1gr1">Kun</div> for(var i=1;i<=10;i++){ $.ajax({ type: "POST", url: "get_url.php", async: false, data: {a: 'hi'} }).d ...

Assign the JSON data to a variable before passing it

Consider the following code snippet: yAxis : { title : { text : 'Exchange rate' }, plotLines : [{ value : 0.6738, color : 'green', dashStyle : 'shortdash', width : 2, ...

Using the following-sibling selector in Java, you can easily click on all <li> elements within a <ul> tag

I am trying to navigate through the list of li elements starting from the "Mentorship" tag link <ul _ngcontent-cwp-c18="" class="navigation clearfix"> <li _ngcontent-cwp-c18="" routerlinkactive="current" ...

ways to incorporate searching within JSON data using AJAX and jQuery in JavaScript

My search box needs to have JSON data appended into it. Below is the HTML code: <input type="search" id="merchantName" name="merchant" placeholder="enter merchant name"></input> I have JSON data containing merchant names that I want to appen ...