jQuery not running, no error message. Syntax error?

Good morning,

jsbin attached: http://jsbin.com/kasofufuni/edit?html,css,js,console,output

I'm trying to make a simple show/hide function. I've pared it down to what you can see in the code above, but some sort of syntax error is causing it to not run or give an error. I've been mashing at it for a couple hours. Any ideas?

I got it to work once in a console, but I have not been able to reproduce.

 $(document).ready(function(){
    $('nav#global').prepend('<span id="menu-icon">Menu</span> <span id="search-icon">Search</span>');

 });   

$('#menu-icon').on('click',function(){
    $('nav#global').prepend('You clicked it'); 
});

Answers


Your click event code is outside of document.ready so it runs before element exists

Change to:

$(document).ready(function(){
     $('nav#global').prepend('<span id="menu-icon">Menu</span> <span id="search-icon">Search</span>');  


    $('#menu-icon').on('click',function(){
        $('nav#global').prepend('You clicked it'); 
    });

 });

Code will run fine in console because element exists then


Need Your Help

C# Synchronizing Threads depending on string

c# multithreading thread-synchronization

In my program I need to process files. My program could use several threads to process files and therefore I need some sort of locking, because each file should not be processed by more than one th...

Change text in regex to upper-case

c# .net

I have a problem. How can I change text between "&lt;" and ">" (HTML tags), to upper-case letters?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.