jQuery click event doesn't seem to work


Akahne Saikyo

I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code:

    function ud(n){
    $("#display").append += n;
}
function ans(c){
    c = eval($("#display").html);
    $("#display").append = c; 
}
function clc(){
    $("#display").append = '';
}

$("#button0").click(ud(0))
$("#button1").click(ud(1));
$("#button2").click(ud(2));
$("#button3").click(ud(3));
$("#button4").click(ud(4));
$("#button5").click(ud(5));
$("#button6").click(ud(6));
$("#button7").click(ud(7));
$("#button8").click(ud(8));
$("#button9").click(ud(9));

$("#addButton").click(ud('+'));
$("#subtractButton").click(ud('-'));
$("#multiplyButton").click(ud('*'));
$("#clearButton").click(clc());
$("#equalsButton").click(ans());
$("#divideButton").click(ud('/'));

Here is the HTML:

    <tr>
  <td colspan="4"><input id="display" name="display" disabled></input></td>
</tr>

<tr>
  <td><button id="button1" value="1">1</button></td>
  <td><button id="button2" value="2">2</button></td>
  <td><button id="button3" value="3">3</button></td>
  <td><button id="addButton">+</button></td>
</tr>
<tr>
  <td><button id="button4" value="4">4</button></td>
  <td><button id="button5" value="5">5</button></td>
  <td><button id="button6" value="6">6</button></td>
  <td><button id="subtractButton">-</button></td>
</tr>
<tr>
  <td><button id="button7" value="7">7</button></td>
  <td><button id="button8" value="8">8</button></td>
  <td><button id="button9" value="9">9</button></td>
  <td><button id="multiplyButton">*</button></td>
</tr>
<tr>
  <td><button id="clearButton">C</button></td>
  <td><button id="button0" value="0">0</button></td>
  <td><button id="equalsButton">=</button></td>
  <td><button id="divideButton">&#247;</button></td>
</tr>

Can someone explain to me what I'm doing wrong? Is there an easier way? Thank you in advance

Alvin

I modify your code and refactor. It should work.

Included some bad syntax in the original code:

  1. Input fields are supposed to set values, so you should use val(), not append(), and the usage of append() is wrong in your code...

  2. click event pass parameter, see jQuery click-eventData-handler and don't want to pass executed function.

function ud(e) {
    var currentVal = $("#display").val();
    $("#display").val(currentVal + e.data.n);
}

function ans(c) {
 var result = eval($("#display").val());
 $("#display").val(result);
}

function clc() {
    $("#display").val('');
}

for(var i = 0; i < 10; i++) {
  $('#button' + i).click({n: i}, ud);
}

$("#addButton").click({n: '+'}, ud);
$("#subtractButton").click({n: '-'}, ud);
$("#multiplyButton").click({n: '*'}, ud);
$("#divideButton").click({n: '/'}, ud);
$("#clearButton").click(clc);
$("#equalsButton").click(ans);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<table>
  <tr>
    <td colspan="4"><input id="display" name="display" disabled></input></td>
  </tr>
  <tr>
    <td><button id="button1" value="1">1</button></td>
    <td><button id="button2" value="2">2</button></td>
    <td><button id="button3" value="3">3</button></td>
    <td><button id="addButton">+</button></td>
  </tr>
  <tr>
    <td><button id="button4" value="4">4</button></td>
    <td><button id="button5" value="5">5</button></td>
    <td><button id="button6" value="6">6</button></td>
    <td><button id="subtractButton">-</button></td>
  </tr>
  <tr>
    <td><button id="button7" value="7">7</button></td>
    <td><button id="button8" value="8">8</button></td>
    <td><button id="button9" value="9">9</button></td>
    <td><button id="multiplyButton">*</button></td>
  </tr>
  <tr>
    <td><button id="clearButton">C</button></td>
    <td><button id="button0" value="0">0</button></td>
    <td><button id="equalsButton">=</button></td>
    <td><button id="divideButton">&#247;</button></td>
  </tr>
</table>

Related


jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

Event click doesn't seem to work in Ruby on Rails

username I have the following question. I created clase .save file and then added it to the event but nothing works code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

Event click doesn't seem to work in Ruby on Rails

username I have the following question. I created clase .save file and then added it to the event but nothing works code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

Event click doesn't seem to work in ruby's orbit

username I have the following question. I created a clase .save and then added event click to it but it didn't work code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

Event click doesn't seem to work in ruby's orbit

username I have the following question. I created a clase .save and then added event click to it but it didn't work code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

jQuery .off("click") event doesn't work

basic need help This has been bugging me for hours and still confused as to why it doesn't work... When I use table.off("click");it , it also unbinds the click event of the first child without id=multi. var table = $("table#datatable") var button = $(".generat

jQuery click event doesn't work

mightyspaj3 I have this simple jQuery click event and for some reason it's not working. works, I can use this inside the $(document).ready(function() {});alert; however the actual click event doesn't work. Thanks in advance and please take it easy on me as I d

MathJax doesn't work in Jquery .click event

Glagas Incoming MathJaxJquery .clickevent doesn't work inside but outside that function. Run the code snippet to see the effect. $('#math').click(function(e){ e.preventDefault(); $("#result").html("$$ MathJax inside .click event doesn't work $$"); }); //outsid

jQuery Click event doesn't work on mobile

lkw3274 I'm trying to make the following JSFiddle work for tablet/mobile (eg "touch" and "click"). https://jsfiddle.net/lkw274/7zt1zL0g/87/ <div class="user-navigation"> <a class="mobile-menu-new" href=""><span></span>Menu</a> </div> $(document).ready

click event on jQuery doesn't work

Creep to death I have printed the echo with php of the input element and I need to get the click event id using jquery. Below is the code I am trying to get API.php (Echo elements to index.php using ajax): echo"<input type='submit' id='something' value='accept

JQuery click() event listener doesn't work

it consolidates I'm trying to get a small project going, but I'm stuck at a very annoying thing. $(document).ready(function() { $("#search-button").click(console.log('hello')) }); As you can see, I'm targeting a search button using the id, and search-buttona

jQuery .off("click") event doesn't work

basic need help This has been bugging me for hours and still confused as to why it doesn't work... When I use table.off("click");it , it also unbinds the click event of the first child without id=multi. var table = $("table#datatable") var button = $(".generat

click event on jQuery doesn't work

Creep to death I have printed the echo with php of the input element and I need to get the click event id using jquery. Below is the code I am trying to get API.php (reflects elements to index.php using ajax): echo"<input type='submit' id='something' value='ac

MathJax doesn't work in Jquery .click event

Glagas Incoming MathJaxJquery .clickevent doesn't work inside but outside that function. Run the code snippet to see the effect. $('#math').click(function(e){ e.preventDefault(); $("#result").html("$$ MathJax inside .click event doesn't work $$"); }); //outsid

jQuery's click event doesn't work

username I can't find the reason why my code is not working. When I click on the generated element, the alert doesn't fire. Here is a fiddle : http://jsfiddle.net/pZAdP/ and code <button id="addMenuItem">Add Menu Item</button> <div id="content"></div> functio

jQuery's click event doesn't work

Alvin Karimi I wrote this code, I need to use jquery to handle click event, but it doesn't work in any browser, when I click on any element, nothing happens. div has no z-index in CSS; The vulnerability "news" element is dynamically added this is the html code

jQuery code for click event doesn't work

Kleber Mota In my current spring project I have a jsp page with the following: <c:url value="/${command['class'].simpleName}/index" var="index"/> <button type="button" class="btn btn-default action" data-url="${index}">Voltar</button> <c:url value="/${command

jQuery's click event doesn't work

username I can't find the reason why my code is not working. When I click on the generated element, the alert doesn't fire. Here is a fiddle : http://jsfiddle.net/pZAdP/ and code <button id="addMenuItem">Add Menu Item</button> <div id="content"></div> functio

jQuery code for click event doesn't work

Kleber Mota In my current spring project I have a jsp page with the following: <c:url value="/${command['class'].simpleName}/index" var="index"/> <button type="button" class="btn btn-default action" data-url="${index}">Voltar</button> <c:url value="/${command

jQuery code for click event doesn't work

Kleber Mota In my current spring project I have a jsp page with the following: <c:url value="/${command['class'].simpleName}/index" var="index"/> <button type="button" class="btn btn-default action" data-url="${index}">Voltar</button> <c:url value="/${command

jQuery function .on() doesn't work with "click" event

ihor I'm using MS Dynamics CRM 2013 and I have the following problem: When I want to add an event handler to the "Documents" button in the navigation pane, The jQuery function .on() does not work with the "click" event. It works fine with "mouseover" or "mouse

Click event by name doesn't work in jQuery

Lockx I'm stuck with a small problem. <a href="#" class="btn btn-small btn-danger" name="ignore" id="45"><i class= "fa fa-close"> </i> Ignore </a> <a title="ignore1" data-original-title="ignore1" id="14" name="ignore1" class="btn btn-small btn-danger" href="#

jQuery click event doesn't work

mightyspaj3 I have this simple jQuery click event and for some reason it's not working. works, I can use this inside the $(document).ready(function() {});alert; however the actual click event doesn't work. Thanks in advance and please take it easy on me as I d

jQuery Click event doesn't work on mobile

lkw3274 I'm trying to make the following JSFiddle work for tablet/mobile (eg "touch" and "click"). https://jsfiddle.net/lkw274/7zt1zL0g/87/ <div class="user-navigation"> <a class="mobile-menu-new" href=""><span></span>Menu</a> </div> $(document).ready

Click event in jQuery doesn't work

Anna Glam I got a div with default class 'unlock' and a text called 'UNLOCK'. When I click on it, it says "UNLOCK" while removing the "unlock" class and adding the "lock" class. html <div class='unlock'>UNLOCKED</div> this work $('.unlock').click(function(){

jQuery click event doesn't work

mightyspaj3 I have this simple jQuery click event and for some reason it's not working. works, where I can use the alert; but the actual click event doesn't work.$(document).ready(function() {}); Thank you in advance and please take it easy on me as I don't ha

jQuery Click event doesn't work on mobile

lkw3274 I'm trying to make the following JSFiddle work for tablet/mobile (eg "touch" and "click"). https://jsfiddle.net/lkw274/7zt1zL0g/87/ <div class="user-navigation"> <a class="mobile-menu-new" href=""><span></span>Menu</a> </div> $(document).ready

jQuery .off("click") event doesn't work

basic need help This has been bugging me for hours and still confused as to why it doesn't work... When I use table.off("click");it , it also unbinds the click event of the first child without id=multi. var table = $("table#datatable") var button = $(".generat