Solved

JSON: Using append to create inline menu from JSON

Posted on 2014-01-10
4
253 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now