Solved

JSON: Using append to create inline menu from JSON

Posted on 2014-01-10
4
250 Views
Last Modified: 2014-01-10
Hello,

I am reading in JSON and appending to a "menu" div on my page.  I cannot figure out how to get each nested div within the menu div to add "inline" as I want to use it as a menu.  They all show up on top of eachother.  Code is below.  

Many thanks!

$.each(result.rows, function(i,val){
mymenu = '<div id="'+result.rows[i].abbr+'" class="menu">'+result.rows[i].abbr+' | </div>';

$(mymenu).appendTo('#menu');	
$("#"+result.rows[i].abbr).on('click',function(){

        alert("HELLO");
});

Open in new window

0
Comment
Question by:1Cougar
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Add this CSS :
#menu div.menu {
    display: inline-block;
}

Open in new window


or replace your div by span

Test page : http://jsfiddle.net/4zh3m/
0
 

Author Comment

by:1Cougar
Comment Utility
Hi,

Thanks a lot.  In your example, you have

$("#menu div.menu").on('click', function(){
alert("HELLO");
});

Open in new window



Actually, I would like an onclick even for each menu item
onclick="doMenu(result.rows[i].id)" 

Open in new window

 
I tried to add it originally on the "mymenu" line but the onclick event never fired.  

With your method I think you are adding the "onclick" event after all of the text has been put in the menu, but I would like something different to occur for "en" and "fr"...

?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
Comment Utility
$("#menu div.menu").on('click', function(){

var fr_or_en = $(this).text();

alert(fr_or_en);

});

Open in new window

0
 

Author Comment

by:1Cougar
Comment Utility
Works great!  Thanks!

Cheers,
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery .hide 1 18
Scrolling the scrollbar in a list in html 9 25
Correct this jQuery code for opening div. 24 10
Bootstap Icons 3 18
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now