Solved

JSON: Using append to create inline menu from JSON

Posted on 2014-01-10
4
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

691 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