The menu bar created from getJSON is not displaying the jQuery UI CSS styles as expected

I have been working on creating a jQueryUI menubar using JSON data. The JSON data is successfully being converted into valid HTML, but the menubar does not display with the desired styling. It appears as an unstyled unordered list rather than a styled menubar like the one shown on . Currently, the routine I am using only goes one level deep for menus, so sub-menu options are intentionally left out.

Interestingly, when I manually input the same HTML code that is generated by the jQuery method in menuBar.jsp, the menubar styles appear correctly.

Below is the main page (appFrameNew.jsp):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>    

<title>Menu Test</title>

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.8.18/themes/base/jquery-ui.css" />
    <link rel="stylesheet" href="http://view.jqueryui.com/menubar/themes/base/jquery.ui.menu.css" />
    <link rel="stylesheet" href="http://view.jqueryui.com/menubar/themes/base/jquery.ui.menubar.css" />

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>    
    <script src="http://view.jqueryui.com/menubar/ui/jquery.ui.core.js" type="text/javascript"></script>
    <script src="http://view.jqueryui.com/menubar/ui/jquery.ui.widget.js" type="text/javascript"></script>
    <script src="http://view.jqueryui.com/menubar/ui/jquery.ui.menu.js" type="text/javascript"></script>
    <script src="http://view.jqueryui.com/menubar/ui/jquery.ui.menubar.js" type="text/javascript"></script>
    <script src="http://view.jqueryui.com/menubar/ui/jquery.ui.position.js" type="text/javascript"></script>

    </head>
    <body>

    <div id="layout" class="layout">

       <jsp:include page="menuBar.jsp" flush="true" />

    </div>

    </body>
    </html>

Next is the content of menuBar.jsp:

<div class="demo"></div>

<script type="text/javascript">
    function select(event, ui) {
        if (ui.item.text() == 'Quit') {
            $(this).menubar('destroy');
        }
    }
    $(function() {
        $("#bar1").menubar({
            position: {
                within: $(".demo").add(window).first()
            },
            select: select
        });
    });

$.getJSON('menuBarTestJSON.json', function(data) {
    var output='<ul id="bar1" class="menubar">';
    for (var i in data.menuOptions) {
        output+='<li><a href="#">' + data.menuOptions[i].menubarItem + '</a></li>';
    }
    output+="</ul>";
    $(".demo").html(output);
});

</script>

<ul id="bar1" class="menubar">
    <li>
        <a href="#File">File</a>
        <ul>
            <li><a href="#Open">Open</a>
            <li><a href="#Save">Save</a></li>
        </ul>
    </li>
</ul>

Lastly, here is the content of menuBarTestJSON.json:

{"menuOptions":[
    {
        "menubarItem":"File",
        "menuElement": {
            "option":"Open",
            "option":"Save",
            "option":"Close",
            "option":"Quit"
        }
    }
]}

I have been struggling to find a solution online for a few days now and would greatly appreciate any assistance. Thank you for your help.

Answer №1

In order to make the jQuery UI menu work properly, you have two options:

  1. Include the code that initializes jQuery UI menu in the getJSON callback function.
  2. Utilize http://api.jquery.com/live/ to monitor changes in the DOM and initialize jQuery UI menu accordingly.

The issue here is that $("#bar1").menubar only triggers once when ready. If you update its contents, it doesn't automatically refresh. You will need to manually reinitialize the menu or set it up to detect changes.

For example:

<script type="text/javascript">
    function select(event, ui) {
        if (ui.item.text() == 'Quit') {
            $(this).menubar('destroy');
        }
    }
    function initMenu() {
        $("#bar1").menubar({
            position: {
                within: $(".demo").add(window).first()
            },
            select: select
        });
    }
    $(function() { initMenu(); });

   $.getJSON('menuBarTestJSON.json', function(data) {
       var output='<ul id="bar1" class="menubar">';
       for (var i in data.menuOptions) {
            output+='<li><a href="#">' + data.menuOptions[i].menubarItem + '</a></li>';
       }
       output+="</ul>";
       $(".demo").html(output);
       initMenu();
   });

</script>

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

Encountered an issue when attempting to create a new rule with AWS IoT

After executing the following command, I am in the process of creating a new rule: aws iot create-topic-rule --rule-name my-rule --topic-rule-payload file://myrule.json The contents of the myrule.json file are as follows: { "sql": "SELECT * FROM &apos ...

Discovering the appropriate use cases for BeanSerializer and MapSerializer is a skill that Jackson must learn

I have a bean that returns a Map as a response, and this bean is accessed through a REST interface using RESTEasy. Instead of using MapSerializer, the jackson library internally uses BeanSerializer to send the response back. Is there any annotation being ...

Incorporating a splash of color while changing the background color on scroll

Is it possible to include more colors between the "beginning-color" and the "ending-color" in this code that changes the background color on scroll? example $(document).ready(function(){ var scroll_pos = 0; var animation_begin_pos = 0; ...

Issues with implementing @font-face on a singular font

I've encountered an issue while setting up a website for a client concerning the fonts. Most fonts are displaying correctly, but there is one font, "chunkfive", that is not working as expected. The problem becomes more severe in Internet Explorer wher ...

Issue with Ajax request in Laravel failing to function when ad blocker is active

While utilizing Laravel to send Ajax requests, everything functions properly on localhost. However, once I test it on the live server with an adblocker enabled, it fails to work. Once the adblocker is disabled, the functionality returns. domain.com/sponso ...

Struggling to convert a JSON string into a Python object with jsonpickle

Below is the structure of my class: class HelloWorld (object): def __init__(self, name, i, id): self.name = name self.i = i self.id = id I then create an object: p = HelloWorld('pj', 3456, '1234') In anot ...

When viewed on a mobile device, the page defaults to displaying the NumPad instead of the Text Keyboard in Angular

The email field in my angular app is opening a Key Pad in mobile view and I'm not sure why. <form (ngSubmit)="onSubmit()" #emailForm="ngForm"> <div class="emailaddress" style="text-align:center;" *ngIf="!showEmail"& ...

The current situation is not effective; it is causing an error saying "Uncaught TypeError: Cannot read property 'substring' of undefined"

Encountering an error "Uncaught TypeError: Cannot read property 'substring' of undefined" while debugging in Chrome Inspector, with the following code: <script type="text/javascript"> $(function countComments() { var mcount = '/ ...

Unable to retrieve JSON element using Fetch

I'm attempting to retrieve a JSON file and exhibit its components within a div. Here is the JSON data I have: [ { "0":{ "host_id":"129230780", "host_names":"STK Homes", ...

Switch button while moving cursor

I'm struggling with getting this 2048x512 image, which has 4 stages of transition, to work properly. https://i.sstatic.net/1PBvX.png While I know how to switch it to the final stage on hover, I can't seem to figure out how to incorporate a trans ...

Retrieving information from a php script using javascript

Recently, I've been attempting to retrieve a PHP response within my JavaScript code. In PHP, the code appears as follows: GetChoice() is not returning anything. Can you help me identify the issue?</p> Update: It turns out that everything work ...

Comparison of jQuery, AngularJS, and Node.js

I'm a beginner in web development and I have some basic knowledge: HTML - structure of websites CSS - design aspect JavaScript - for adding interactivity Now, what exactly is jQuery, AngularJS, and Node.js? Upon researching, I discovered that jQue ...

The Jquery Index() method often results in a return value of -

I have developed a function in JavaScript that creates HTML content. Within the test(test1) function, I am checking if test1.Test__c is null and changing the color accordingly. The line ".table-striped > tbody > tr.testcss" is responsible for changin ...

Unable to invoke a function in TypeScript from a Kendo template within the Kendo TreeList component

In my TypeScript file for class A, I am encountering an issue with the Kendo TreeList code. I am trying to call a function from the Kendo template. export class A{ drillDownDataSource: any; constructor() { this.GetStatutoryIncomeGrid ...

Retrieving text content from multiple classes with a single click event

There are numerous elements each having class names p1, p2, p3,...p14. Consequently, when attempting to extract text from the clicked class, text from all classes is retrieved! For instance, if the expected text is 80, it ends up being 808080080808080808 ...

Modification of encapsulated class in Angular is not permitted

Within Angular, a div element with the class "container" is automatically created and inserted into my component's HTML. Is there a way to modify this class to "container-fluid"? I understand that Angular utilizes encapsulation, but I am unsure of how ...

What is the best way to dynamically write a knockout data binding event using jQuery?

let $button = $('<input/>').attr({ type: 'button', value: data.styleData, id: buttonId, data - bind: event: { click: $parent.submitPopUp } }); An error is being displayed. ...

getting the child td element within a tr using a variable that changes dynamically

I'm facing an issue where I am trying to access the <td> of a <tr> using the jQuery .eq() function. The code snippet below illustrates what I am attempting: var foundElement = 3; var values = $(this).children("td:nth-child('" + foun ...

Populate an HTML select with data as users click the create button

Encountering an issue when loading data into an HTML select after users press or click a button. The situation is as follows: A button is available to create another button dynamically Upon clicking the created button, a form is displayed Once the form i ...

All you need to know about the jQuery .css method

I've been struggling to get this script to function properly. Despite searching online, I haven't been able to find a satisfactory solution. $( document ).ready(function() { $("#container").click(function(){ var color = $(this).css("backgro ...