0

I want to hide a button on click using jquery and not using inline functions.

I have the HTML:

<button id="mybutton" value="click me">click me</button>

and the JS:

$("#mybutton").on("click", hideelement);

hideelement = function() {
    $(this).hide();
}

the button does not hide on click. What am I doing wrong?

showdev
  • 28,454
  • 37
  • 55
  • 73
  • 1
    possible duplicate of [Hiding button using jquery](http://stackoverflow.com/questions/12790297/hiding-button-using-jquery) – Max Aug 28 '14 at 21:13
  • Don't re-invent the wheel: `$("#mybutton").on("click", function () { $(this).hide(); }); ` – emerson.marini Aug 28 '14 at 21:15
  • `$("#mybutton").click(function() { $(this).hide(); });` – user13286 Aug 28 '14 at 21:15
  • If you look in your browser console, I'm pretty sure there is an error there. – emerson.marini Aug 28 '14 at 21:16
  • Your hideelement function is unclosed. No reason to define it as a separate function anyway. – user13286 Aug 28 '14 at 21:17
  • 1
    @MelanciaUK: Anonymous functions are hard to unit test. Named functions are not only good practice but also help you writing testable code and support separating DOM manipulation from business logic. Also `this` is perfectly preserved using a named function in this scenario. – Nope Aug 28 '14 at 21:24
  • @FrançoisWahl I got your point, but considering that the only manipulation here is to hide an element, I think it's a waste to create a separate function. – emerson.marini Aug 28 '14 at 21:26

5 Answers5

3

If you'll see the accepted answer for this question you'll know what is the problem

Problem is when you define a function like

var funcName = function(){...}

So in this case you have to call this function after definition. Otherwise it won't be available.

define your function like bellow

function hideelement() {
    $(this).hide();
}

DEMO

OR change the order like this

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 

DEMO

Community
  • 1
  • 1
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
3

To add to the already working solutions with a little more detail.


Document Ready


First, make sure your code is either placed at the bottom of your body or inside a document ready, similar to the below, to make sure the element is in the DOM at the time your code executes.:

$(document).ready(function(){
   ... your code here...
});

$("#mybutton") will otherwise not be in the DOM yet when the code executes and no event will be bound.


Variable hoisting


If that is not the issue and the event is bound you may find that the way your code is written causes the error: Uncaught ReferenceError: hideelement is not defined

The reason for that is variable hoisting

$("#mybutton").on("click", hideelement);

hideelement = function(){
    $(this).hide();
}

The above code is actually interpreted by the JavaScript interpreter as follows:

var hideelement; // declaration was hoisted to the top of the scope

$("#mybutton").on("click", hideelement); // at this point hideelement is still 'undefined'

hideelement = function(){ // assignment of the value stays within the lexical scope
    $(this).hide();
}

JavaScript will hoist the declaration to the top of the current scope but will leave the assignment in the lexical scope where it was defined.

However, if you change your declaration to this:

$("#mybutton").on("click", hideelement);

function hideelement(){
    $(this).hide();
}

JavaScript now interprets the above code as follows:

function hideelement(){
    $(this).hide();
}

$("#mybutton").on("click", hideelement); // hideelements is defined

As hideelements is no longer an assignment but only a declaration the complete function will be hoisted and as such is defined at the time it is used by your event binding.


DEMO


Naturally off course the already suggested solution in the other answer of defining your assignment lexically before the use of it will also work. i,e:

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 

For the sake of simplicity I intentionally did not go into global scope and the differences with using var.

Community
  • 1
  • 1
Nope
  • 22,147
  • 7
  • 47
  • 72
0
$( "#mybutton" ).click(function() {
    $( this ).hide();
});

http://jsfiddle.net/17n2faLo/

patricmj
  • 365
  • 2
  • 3
  • 18
-1

try this $("#mybutton").click(function(){ $(this).hide() });

user3349436
  • 151
  • 5
-1

I had been looking too hard at the screen. After reversing the function definition and setting the on-click behavior, it worked.

hideelement = function() {
    $(this).hide();
}

$("#mybutton").on("click", hideelement); 
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64