I'm trying to build a simple calculator, yet I was wondering how I can reassign the input field. Right now I get the following error-message:
"message": "Uncaught TypeError: res.split is not a function",
Basically I would like to split the string when the equal button is pressed and just show the result in the input field. Just imagine that something like '12-3' is typed in.
$('.equal').on('click', () => {
let res = ($('.inp').val(value))
let spl = res.split('-')
$('.inp').val(spl);
However, as I'm new to jquery and javascript, I don't really understand why I'm getting the error message above.
Thanks so much for reading or even helping a beginner out! For now, only the button '1', '2', '3', '-' and '=' work.
$(document).ready(() => {
let numb1 = '1';
let value="";
$('.1').on('click', () => {
$('.inp').val(value += numb1)
})
let numb2 = '2';
$('.2').on('click', () => {
$('.inp').val(value += numb2)
})
let numb3 = '3';
$('.3').on('click', () => {
$('.inp').val(value += numb3)
})
let minus = '-';
$('.min').on('click', () => {
$('.inp').val(value += minus)
})
$('.equal').on('click', () => {
let res = ($('.inp').val(value))
let spl = res.split('-')
$('.inp').val(spl);
})
});
.grid {
display: grid;
border: 2px black solid;
border-radius: 5px;
width: 40%;
height: 60%;
/*grid-template-columns: 50% 50%;*/
grid-template: 1fr 1fr / 1fr 1fr 1fr 1fr;
/*rows come first and then columns*/
margin: 10px;
}
.box {
width: 60%;
background-color: lightgray;
margin: 10px;
text-align: center;
border-radius: 5px;
}
.box-gr {
width: 60%;
background-color: darkgray;
margin: 10px;
text-align: center;
border-radius: 5px;
}
.inp {
width: 40%;
margin: 10px;
border-radius: 5px;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel='stylesheet' type='text/css' href='/Users/f/Downloads/Calc-project/style-calc.css'>
<title>Chat-Messenger Window</title>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
</head>
<body>
<input class="inp" type="text"/>
<div class = "grid">
<button class="box-gr">(</button>
<button class="box-gr">)</button>
<button class="box-gr">%</button>
<button class="box-gr">CE</button>
<button class="box 7">7</button>
<button class="box 8">8</button>
<button class="box 9">9</button>
<button class="box-gr">/</button>
<button class="box 4">4</button>
<button class="box 5">5</button>
<button class="box 6">6</button>
<button class="box-gr">x</button>
<button class="box 1">1</button>
<button class="box 2">2</button>
<button class="box 3">3</button>
<button class="box-gr min">-</button>
<button class="box 0">0</button>
<button class="box">.</button>
<button class="box equal">=</button>
<button class="box-gr">+</button>
</div>
</body>
</html>
$('.inp').val(value)is using to set the value for input, you need to use$('.inp').val()