var brukerTall = 0;
var numberArray = [];
var operator;
var temp = 0;
var resultString = "";
var evalString = 0;
// A $( document ).ready() block.
$(function tall (tall) {
brukerTall = tall;
addToResult(brukerTall);
updateViewResult(brukerTall);
});
$("#one").click(function() {
alert("hello"
);
$(function addToResult(temp) {
numberArray.push(temp);
});
$(function sjekkTall(tall) {
addToResult(tall);
var temp = 0;
temp = tall;
updateViewResult(tall)
});
$(function updateViewResult(temp) {
$("#display").append(temp);
});
$(function operators(operator) {
addToResult(operator);
updateViewResult(operator);
});
$(function clearAll() {
$("#display").text("");
});
$(function removeDigit() {
});
$(function fixString() {
resultString = numberArray.toString();
var valueFixed = resultString.replace(/,/g, '');
calculate(valueFixed);
//alert(valueFixed);
});
$(function fixString() {
var resultaValue = parseFloat(eval(valueFixed));
clearAll();
updateViewResult(resultaValue);
});
Can anyone see what is missing here?
Starting with jquery
```
var brukerTall = 0;
var numberArray = [];
var operator;
var temp = 0;
var resultString = "";
var evalString = 0;
// A $( document ).ready() block.
$(function tall (tall) {
brukerTall = tall;
addToResult(brukerTall);
updateViewResult(brukerTall);
});
$("#one").click(function() {
alert("hello"
);
})
$(function addToResult(temp) {
numberArray.push(temp);
});
$(function sjekkTall(tall) {
addToResult(tall);
var temp = 0;
temp = tall;
updateViewResult(tall)
});
$(function updateViewResult(temp) {
$("#display").append(temp);
});
$(function operators(operator) {
addToResult(operator);
updateViewResult(operator);
});
$(function clearAll() {
$("#display").text("");
});
$(function removeDigit() {
});
$(function fixString() {
resultString = numberArray.toString();
var valueFixed = resultString.replace(/,/g, '');
calculate(valueFixed);
//alert(valueFixed);
});
$(function fixString() {
var resultaValue = parseFloat(eval(valueFixed));
clearAll();
updateViewResult(resultaValue);
});
<button onclick="tall(7)" value="7" class="btn btn-primary" id="seven">7</button>
Okey, thanks. I will do that next time.
I know why the alert did not work, it was becuase the onclick was applied kind of after, so therefore the click event function did not have effect, okey thanks @DanLaff .
Do you always need document.ready function when using jquery?
Because everything was working fine without the functions looking like this
$(function addToResult(temp) {
numberArray.push(temp);
});
but this did the job before,
function addToResult(temp) {
numberArray.push(temp);
}
So what is the difference, thanks in advance :)
$('.digit').on('click', function() {
alert($(this).val())
});
so this would actually be considered better practice than having the btn on click on the HTML elements itself, that is intereseting.
And you are saying one of the reasons is because you want to have organised code, and keep the javascript /jquery seperated from the html.. .
Do you know why I cant access the function called tall from the click function here.
If you want an have time, I can share the code I had before with jquery and onclick, so maybe you can look over it and tell me the best way to implement what I want with Jquery?
Everything was working fine before, but the teacher has a test in python that checks for jquery and not just javascript as I have understood it.
function functionOne() {
function functionA() {
// do something
}
function functionB() {
// do something else
}
functionA(); // this will work fine
}
functionA(); // doesn't run because you can't call a function inside another function out here
@DanLaff here you can see how I have done it until now.
the code pen you sent me makes sense, using a for loop to iterate trough the buttons. That was smart.
But what if you would like to send in a value, for example if the user click on 7, like the way I have done it here.
<button onclick="tall(7)" value="7" class="btn btn-primary" id="seven">7</button>
```
if(val.stream === null){
$("#" + username).css("box-shadow", "5px 0px 10px red");
$("#" + username + ">p").text("Offline")
.css("color","red");
} else {$("#" + username).css("box-shadow", "5px 0px 10px green");
$("#" + username + ">p").text("Online")
.css("color","green");
}