Click here to Skip to main content
14,366,428 members

JavaScript

 
AnswerRe: how to get a js fuction to return html opening and closing tag Pin
Richard MacCutchan23-Dec-18 0:35
protectorRichard MacCutchan23-Dec-18 0:35 
GeneralRe: how to get a js fuction to return html opening and closing tag Pin
Member 1409810523-Dec-18 15:31
memberMember 1409810523-Dec-18 15:31 
QuestionGet an html table multiple rows as a string URL for Ajax Pin
Member 1409585019-Dec-18 23:55
memberMember 1409585019-Dec-18 23:55 
AnswerRe: Get an html table multiple rows as a string URL for Ajax Pin
Afzaal Ahmad Zeeshan23-Dec-18 1:29
mveAfzaal Ahmad Zeeshan23-Dec-18 1:29 
Questiondebug shopping cart project Pin
ghazabaz13-Dec-18 16:56
memberghazabaz13-Dec-18 16:56 
Questione.target.value, switch-case operators Pin
ghazabaz6-Dec-18 20:22
memberghazabaz6-Dec-18 20:22 
QuestionRe: e.target.value, switch-case operators Pin
Richard MacCutchan7-Dec-18 0:26
protectorRichard MacCutchan7-Dec-18 0:26 
QuestionRe: e.target.value, switch-case operators Pin
ghazabaz7-Dec-18 4:47
memberghazabaz7-Dec-18 4:47 
Richard, good question. I am following the example shown in the text. E is the buttonValue(e) function's parameter ?

I am guessing the operator required is a + operator to add the value? I also thought I did not need an operator there.

Here is the whole JS file code:

// Loads the init function when the broser window is opened
window.onload = init();

// Declares the init() function
function init() {
	
	// Declares the calcButtons variable and sets it equal to the element calcButton
	var calcButtons = document.getElementsByClassName("calcButton");
	
	// Loops through calcButtons and runs the buttonClick() function when a button is clicked
	for (var i = 0; i < calcButtons.length; i++) {
		calcButtons[i].onclick = buttonClick();
	}
	
	// Runs the calcKeys function when a key is pressed within the calcWindow element
	document.getElementById("calcWindow").addEventListener(onkeydown, calcKeys());
}

// Declares the buttonClick() function
function buttonClick(e) {
	
	
	// Declares variables calcValue, calcDecimal and buttonValues
	var calcValue = document.getElementById("calcWindow").value;
	var calcDecimal = document.getElementById("decimals").value;
	var buttonValue = e.target.value;
	
	// Begins a switch-case structure to assign events to each button pressed
	switch(buttonValue) {
		case "del":
		calcValue = "";
		break;
		case "bksp":
		calcValue = eraseChar(calcValue);
		break;
		case "enter":
		calcValue = " = " + evalEq(calcValue, calcDecimal) + "\n";
		break;
		case "prev":
		calcValue = lastEq(calcValue);
		calcValue = calcValue + buttonValue;
		break;
	}
}


document.getElementById("calcWindow").value = calcValue;
document.getElementById("calcWindow").focus();


// Declares the calcKeys() function
function calcKeys(e) {
	
	var calcValue = document.getElementById("calcWindow").value;
	var calcDecimal = document.getElementById("decimals").value;
	
	
	// Begins a switch-case structure, assigning values to each of the buttons below (delete, enter, arrow up) when pressed
	switch(e) {
		case "Delete": 
		calcValue = "";
		break;
		case: "Enter":
		calcValue = " = " + evalEq(calcValue, calcDecimal);
		break;
		case "ArrorUp":
		calcValue = lastEq(calcWindow.value);
		break;
		
		e.preventDefault();
	}
	

	document.getElementById("calcWindow").value = calcValue;
	
}







/* ===================================================================== */

function eraseChar(textStr) { 
   return textStr.substr(0, textStr.length - 1);
}

function evalEq(textStr, decimals) {
   var lines = textStr.split(/\r?\n/);
   var lastLine = lines[lines.length-1];
   var eqValue = eval(lastLine);
   return eqValue.toFixed(decimals);
}  

function lastEq(textStr) {
   var lines = textStr.split(/\r?\n/);
   var lastExp = lines[lines.length-2];
   return lastExp.substr(0, lastExp.indexOf("=")).trim();
}


modified 7-Dec-18 9:53am.

AnswerRe: e.target.value, switch-case operators Pin
Richard MacCutchan7-Dec-18 5:29
protectorRichard MacCutchan7-Dec-18 5:29 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming7-Dec-18 6:37
mveRichard Deeming7-Dec-18 6:37 
QuestionRe: e.target.value, switch-case operators Pin
ghazabaz7-Dec-18 7:44
memberghazabaz7-Dec-18 7:44 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming7-Dec-18 7:51
mveRichard Deeming7-Dec-18 7:51 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming7-Dec-18 7:54
mveRichard Deeming7-Dec-18 7:54 
QuestionRe: e.target.value, switch-case operators Pin
ghazabaz8-Dec-18 6:19
memberghazabaz8-Dec-18 6:19 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming11-Dec-18 2:16
mveRichard Deeming11-Dec-18 2:16 
PraiseRe: e.target.value, switch-case operators Pin
ghazabaz13-Dec-18 7:05
memberghazabaz13-Dec-18 7:05 
QuestionEasy HTML Editor Pin
Bram van Kampen3-Dec-18 16:23
memberBram van Kampen3-Dec-18 16:23 
AnswerRe: Easy HTML Editor Pin
Nathan Minier6-Dec-18 2:24
professionalNathan Minier6-Dec-18 2:24 
AnswerRe: Easy HTML Editor Pin
Nitin Sawant7-Dec-18 2:57
professionalNitin Sawant7-Dec-18 2:57 
AnswerRe: Easy HTML Editor Pin
RedDk28-Dec-18 12:44
memberRedDk28-Dec-18 12:44 
GeneralRe: Easy HTML Editor Pin
Bram van Kampen28-Dec-18 13:55
memberBram van Kampen28-Dec-18 13:55 
QuestionProblems to obtain value "0" or "1" from a div Pin
serenimus1-Dec-18 1:37
memberserenimus1-Dec-18 1:37 
AnswerRe: Problems to obtain value "0" or "1" from a div Pin
ZurdoDev4-Dec-18 3:27
professionalZurdoDev4-Dec-18 3:27 
QuestionUnable to switch events in jquery Pin
vmann30-Nov-18 19:45
membervmann30-Nov-18 19:45 
SuggestionRe: Unable to switch events in jquery Pin
ZurdoDev4-Dec-18 3:28
professionalZurdoDev4-Dec-18 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.