Parth Parulekar
Parth Parulekar

Reputation: 107

Button Selector --> Adds numbers to Input in Form

I am trying to select all my buttons except for my equal sign for a calculator I am making. I am trying to do this with jQuery.

Before, I used:

function run1() {
    document.blank.result.value += "1";
}

I did this for each button clicked (0-9, +,-,/,*,=)

But I want to use jQuery now. I want to select all those buttons and input them depending on what was selected, excluding "=", as that will not be displayed.

So far, I have this:

$("button").not("#equal").click(function run() {
    result += $("input[value]").text();
});

Ok, so I changed it a bit. Now I have this:

$("button").not("#equal").click(function run() {
result += $("#num").val();

});

I think I got it. Thanks.

Upvotes: 0

Views: 87

Answers (2)

klenium
klenium

Reputation: 2607

$("button").not("#equal").click(function() {
    var result = $("form[name='blank'] [name='result']");
    result.val(parseInt(result.val()) + 1);
    // or result.val(parseInt(result.val()) + parseInt($(this).val()));
    // or result.val(parseInt(result.val()) + parseInt($("input[name='value']").val()));
});

Upvotes: 1

CSturgess
CSturgess

Reputation: 1557

Try instead to do this:

$("button").not("#equal").click(function run(e) {
    result += $(e.target).val()
}

Depending on how your buttons are set up, you could also grab a data-attribute from it instead. e is the event being passed to the function. e.target is the actual button that was clicked.

Upvotes: 1

Related Questions