Solved

JSON: Using append to create inline menu from JSON

Posted on 2014-01-10
4
256 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
ID: 39770982
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
ID: 39771045
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
ID: 39771070
$("#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
ID: 39771398
Works great!  Thanks!

Cheers,
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
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…

805 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