I want to add a text (calculator format) to an input field containing following data:
a digit
a sign (x, -, +, /)
a digit
How to add padding between those elements in one input field, so I could have:
[digit][30px gap][sign][10px gap][2nd digit]
Gaps between digits can be customized either.
Adding simple padding to the input doesn't help... ( https://jsfiddle.net/gzuv6Lr4/ shows it)
Any clues?
._inp {
color: red;
padding-left: 10px;
}
<form>
<input type="text" class="_inp" value="10 x 30" />
</form>
Use the CSS word-spacing property on the input field:
https://developer.mozilla.org/en-US/docs/Web/CSS/word-spacing
Look this solution:
var signsMap = new Map();
signsMap.set(16, "X");
signsMap.set(88, "x");
signsMap.set(107, "+");
signsMap.set(109, "-");
signsMap.set(111, "/");
var expression = document.getElementById("expression");
var numberSequence = false;
expression.addEventListener("keyup", function(e) {
console.log(!isNaN(e.key));
if (signsMap.get(e.keyCode)) {
findSign(signsMap.get(e.keyCode));
numberSequence = false;
} else if (!isNaN(e.key) && !numberSequence) {
var expValue = expression.value;
var head = expValue.substring(0, expression.selectionStart - 1);
var body = expValue.substring(expression.selectionStart - 1, expression.selectionStart);
var tail = expValue.substring(expression.selectionStart, expValue.length);
expression.value = head + " " + body + tail;
numberSequence = true;
} else {
e.preventDefault();
}
}, false);
function findSign(sign) {
var expValue = expression.value;
if (sign) {
if (expValue.indexOf(sign, expression.selectionStart - 1) > -1) {
var head = expValue.substring(0, expression.selectionStart - 1);
var body = expValue.substring(expression.selectionStart - 1, expression.selectionStart);
var tail = expValue.substring(expression.selectionStart, expValue.length);
expression.value = head + " " + body + tail;
}
} else {
for (const sign of signsMap.values()) {
if (expValue.indexOf(sign) > -1) {
expression.value = expValue.replace(sign, " " + sign);
break;
}
}
}
}
window.onload = findSign(false);
._inp {
color: red;
word-spacing: 10px;
}
<form>
<input type="text" id="expression" class="_inp" value="10 x 30" />
</form>
Related
i cant edit my body background or anything about my body from my css file. does anyone know why this might be. I know the CSS file is linked properly as it edits the table. its only the body that wont edit. please can someone tell me why this is happening.
ive had to remove some of the html code as it was aparetly too long however this is unrelated to the issue
<style>
body {
background-color: lightblue;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 5px;
}
</style>
<!DOCTYPE html>
<script src="js.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<link rel="stylesheet" href="css.css">
</link>
<body>
<div1>
<header>
<h1 id="header">Noah's Address book</h1>
</header>
<div id="tableside">
<div id="searchside">
<label for="searching">Search for address</label><br>
<input type="text" id="searching" name="searching" required><br>
</div>
<div id="table">
<table id="table1">
</table>
</div>
</div>
</div1>
<script>
$("#adContact").on("submit", function(event) {
event.preventDefault();
var $fname = $("#fname");
var fname = $fname.val();
var $lname = $("#lname");
var lname = $lname.val();
var $numb = $("#numb");
var numb = $numb.val();
var $adr = $("#adr");
var adr = $adr.val();
if (nameTest(fname, lname) === false) {
$("#error").text("First and Last name must only contain letters");
} else if (numTest(numb) === false) {
$("#error").text("Number can only contain numbers and must be in 00000000000 format");
} else if (firstLength(fname) === false) {
$("#error").text("First name input too long");
} else if (lastLength(lname) === false) {
$("#error").text("Last name input too long");
} else if (addressLength(adr) === false) {
$("#error").text("Address input too long, please keep input below 100 characters ");
} else if (alreadyActive(fname, lname, numb) === false) {
$("#error").text("contact already exists");
} else {
$("#error").text("contact added");
newContact(fname, lname, numb, adr);
}
reload()
})
$("#removeContact").on("submit", function(event) {
event.preventDefault();
var $numb1 = $("#numb1");
var numb1 = $numb1.val();
var $fname1 = $("#fname1");
var fname1 = $fname1.val();
var $lname1 = $("#lname1");
var lname1 = $lname1.val();
if (removeContact(fname1, lname1, numb1) === false) {
$("#error1").text("Contact not found");
} else {
$("#error1").text("Contact removed");
}
reload()
})
var row = "<tr><th>First name</th><th>Last name</th><th>Number</th><th>Address</th></tr>"
$("#table1").append(row)
var contactListOriginall = addressLogLength()
for (let i = 0; i < contactListOriginall.length; i++) {
let contactName = contactListOriginall[i]
var firstname = getData(contactName)[0];
var lastname = getData(contactName)[1]
var number = getData(contactName)[2]
var address = getData(contactName)[3]
let row = "<tr id=contactName><th>" + firstname + "</th><th>" + lastname + "</th><th>" + number + "</th><th>" + address + "</th></tr>"
$("#table1").append(row)
}
function reload() {
$("#table1 tr").remove()
let row = "<tr><th>First name</th><th>Last name</th><th>Number</th><th>Address</th></tr>"
$("#table1").append(row)
var $search = $("#searching");
var search = $search.val();
let contactList = match(search)
for (let c = 0; c < contactList.length; c++) {
let contactName = contactList[c]
var firstname = getData(contactName)[0];
var lastname = getData(contactName)[1]
var number = getData(contactName)[2]
var address = getData(contactName)[3]
let row = "<tr id=contactName><th>" + firstname + "</th><th>" + lastname + "</th><th>" + number + "</th><th>" + address + "</th></tr>"
$("#table1").append(row)
}
}
$("#searching").on("input", function() {
reload()
});
</script>
</body>
You don't need "style" tags because these tags only used in .html file to make .html understand that these are css codes.
Also, you may need to add "head" tags at the bottom and above like that:
<head>
<script src="js.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<link rel="stylesheet" href="css.css">
</link>
</head>
If it still doesn't work add html tags like that:
<!DOCTYPE html>
<html>
<head>
<script src="js.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<link rel="stylesheet" href="css.css">
</link>
</head>
<body>
<div1>
<header>
<h1 id="header">Noah's Address book</h1>
</header>
<div id="tableside">
<div id="searchside">
<label for="searching">Search for address</label><br>
<input type="text" id="searching" name="searching" required><br>
</div>
<div id="table">
<table id="table1">
</table>
</div>
</div>
</div1>
<script>
$("#adContact").on("submit", function(event) {
event.preventDefault();
var $fname = $("#fname");
var fname = $fname.val();
var $lname = $("#lname");
var lname = $lname.val();
var $numb = $("#numb");
var numb = $numb.val();
var $adr = $("#adr");
var adr = $adr.val();
if (nameTest(fname, lname) === false) {
$("#error").text("First and Last name must only contain letters");
} else if (numTest(numb) === false) {
$("#error").text("Number can only contain numbers and must be in 00000000000 format");
} else if (firstLength(fname) === false) {
$("#error").text("First name input too long");
} else if (lastLength(lname) === false) {
$("#error").text("Last name input too long");
} else if (addressLength(adr) === false) {
$("#error").text("Address input too long, please keep input below 100 characters ");
} else if (alreadyActive(fname, lname, numb) === false) {
$("#error").text("contact already exists");
} else {
$("#error").text("contact added");
newContact(fname, lname, numb, adr);
}
reload()
})
$("#removeContact").on("submit", function(event) {
event.preventDefault();
var $numb1 = $("#numb1");
var numb1 = $numb1.val();
var $fname1 = $("#fname1");
var fname1 = $fname1.val();
var $lname1 = $("#lname1");
var lname1 = $lname1.val();
if (removeContact(fname1, lname1, numb1) === false) {
$("#error1").text("Contact not found");
} else {
$("#error1").text("Contact removed");
}
reload()
})
var row = "<tr><th>First name</th><th>Last name</th><th>Number</th><th>Address</th></tr>"
$("#table1").append(row)
var contactListOriginall = addressLogLength()
for (let i = 0; i < contactListOriginall.length; i++) {
let contactName = contactListOriginall[i]
var firstname = getData(contactName)[0];
var lastname = getData(contactName)[1]
var number = getData(contactName)[2]
var address = getData(contactName)[3]
let row = "<tr id=contactName><th>" + firstname + "</th><th>" + lastname + "</th><th>" + number + "</th><th>" + address + "</th></tr>"
$("#table1").append(row)
}
function reload() {
$("#table1 tr").remove()
let row = "<tr><th>First name</th><th>Last name</th><th>Number</th><th>Address</th></tr>"
$("#table1").append(row)
var $search = $("#searching");
var search = $search.val();
let contactList = match(search)
for (let c = 0; c < contactList.length; c++) {
let contactName = contactList[c]
var firstname = getData(contactName)[0];
var lastname = getData(contactName)[1]
var number = getData(contactName)[2]
var address = getData(contactName)[3]
let row = "<tr id=contactName><th>" + firstname + "</th><th>" + lastname + "</th><th>" + number + "</th><th>" + address + "</th></tr>"
$("#table1").append(row)
}
}
$("#searching").on("input", function() {
reload()
});
</script>
</body>
</html>
Remove the surrounding <style> tags as they aren't needed because you are already in a styles file.
Inside css.css:
body {
background-color: lightblue;
}
table,
th,
td {
border: 1px solid black;
}
th,
td {
padding: 5px;
}
Here I tried to push my elements into array but I am not getting output in the p tag. May be there is any mistake in the code. Can someone help me out ?
<script type="text/javascript">
jQuery(document).ready(function()
{
var arr = [];
var text = "";
jQuery("#txtResult").click(function()
{
//var text = "";
//var string = jQuery("#txtPrint").html();
for (i = 0; i < arr.length; i++)
{
if (jQuery("#txtFirstNo").val().length == 5)
{
arr.push("QQQ");
}
if (jQuery("#txtFirstNo").val().length == 5)
{
arr.push("AA");
}
text += arr[i] + "<br>";
}
jQuery("p").html(text);
});
});
</script>
</head>
<body>
<input type="text" id="txtFirstNo" name="txtFirstNo" placeholder="Enter value" />
<input type="submit" id="txtResult"/>
<p id="txtPrint"></p>
</body>
</html>
Simplified, looks like you just wanted smth like this
$("#txtResult").click(function(){
let text = "";
if ($("#txtFirstNo").val().length == 5) {
text = "QQQ" + "<br>" + "AA" + "<br>";
}
$("p").html(text);
}
I'm doing number formatting in Angular JS for account numbers for a banking application.
How can I implement account format 11-02980-1. I want the delimiter (-) to be added automatically to the input text when some adds the numbers (11029801). i.e when i type 11, the delimiter (-) is added and then 02980, another delimiter is added and then lastly 1
Thanks
EDIT
Is this what you were looking for?
Try this snippet:
var MyApp = angular.module('MyApp', []);
MyApp.controller('TestController', TestController);
function TestController($scope) {
$scope.accNumber = '';
$scope.previousAccNumber = '';
$scope.editAccountNumber = function (accNumber) {
if ($scope.previousAccNumber.length > accNumber.length) {
$scope.previousAccNumber = accNumber;
return accNumber;
}
accNumber = accNumber.replace(/-/g, '');
var classA = accNumber.slice(0, 2);
var classB = accNumber.slice(2, 7);
var classC = accNumber.slice(7, 8);
var finalNumber = [];
if (classA && classA.length == 2) {
finalNumber.push(classA, "-");
} else {
return classA;
}
if (classB && classB.length == 5) {
finalNumber.push(classB, "-");
} else {
return classA + "-" + classB;
}
if (classC && classC.length == 1) {
finalNumber.push(classC);
}
$scope.previousAccNumber = finalNumber.join('');
return finalNumber.join('');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="TestController">
<input id="accNumber" type="text" placeholder="Enter Account Number" ng-model="accNumber" ng-change="accNumber = editAccountNumber(accNumber)" />
</div>
</div>
I am currently expanding and collapsing the height of a text area on the keyupevent. However I want the text area also to initialise it's height once the value is binded to the text area via a knockout custom binding. Any solutions?
(With the use of only javascript) (Without adding any jquery libraries)
Current key up handelling code
var textElement = $textBox.get(0);
var textElementOriginalHeight = $textBox.height();
while ($textBox.height() > textElementOriginalHeight && textElement.scrollHeight < textElement.offsetHeight) {
$textBox.height($textBox.height() - 1);
}
var h = 0;
while (textElement.scrollHeight > textElement.offsetHeight && h !== textElement.offsetHeight) {
h = textElement.offsetHeight;
$textBox.height($textBox.height() + 1);
}
You're gonna need to register a custom binding-handler to do that. Something like:
(function(ko)
{
function handleAutoFit(textElement, val)
{
if (!textElement.value)
textElement.value = val;
var $textBox = $(textElement);
var textElementOriginalHeight = $textBox.height();
while ($textBox.height() > textElementOriginalHeight && textElement.scrollHeight < textElement.offsetHeight) {
$textBox.height($textBox.height() - 1);
}
var h = 0;
while (textElement.scrollHeight > textElement.offsetHeight && h !== textElement.offsetHeight) {
h = textElement.offsetHeight;
$textBox.height($textBox.height() + 1);
}
}
ko.bindingHandlers.autoFit = {
update: function (element, valueAccessor) {
var val = ko.unwrap(valueAccessor());
handleAutoFit(element, val);
}
};
})(ko);
HTML:
<textarea data-bind="autoFit: someObservable, value: someObservable, valueUpdate: 'afterkeydown'"></textarea>
Or if you're using the Knockout 3.1 and above:
<textarea data-bind="autoFit: someObservable, textInput: someObservable"></textarea>
See Fiddle
I have a countdown function (the code for which, is below) that is small and alligned to the top left corner.
I would like to be able to customize the size, allignment, back color, fore color, and also have a button to stop, start, and clear the timer.
Secondly, I want the counter to show "00:00:00" at the start, and when it reaches "00:00:59", I want it to roll over to "00:01:00" showing that 1 minute has passed.
I would think that CSS/HTML would be the most appropriate language to do this in to acheive the desired visual effect but Javascript seems to have more arithmatic/parsing functionality.
<br><script language="JavaScript">
function calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1))%num2).toString();
if (LeadingZero && s.length < 2) {
s = "0" + s;
return "<b>" + s + "</b>";
}
}
function CountBack(secs) {
if (secs < 0) {
document.getElementById("cntdwn").innerHTML = FinishMessage;
return;
}
DisplayStr = DisplayFormat.replace(/%%D%%/g,calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
document.getElementById("cntdwn").innerHTML = DisplayStr;
if (CountActive) {
setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}
}
function putspan(backcolor, forecolor) {
document.write("<span id='cntdwn' style='background-color:" + backcolor + "; color:" + forecolor + "'></span>");
}
if (typeof(BackColor)=="undefined") {
BackColor = "black";
}
if (typeof(ForeColor)=="undefined") {
ForeColor= "white";
}
if (typeof(TargetDate)=="undefined") {
TargetDate = "12/31/2050 5:00 AM";
}
if (typeof(DisplayFormat)=="undefined") {
DisplayFormat = "%%H%%:%%M%%:%%S%%";
}
if (typeof(CountActive)=="undefined") {
CountActive = true;
}
if (typeof(FinishMessage)=="undefined") {
FinishMessage = "";
}
if (typeof(CountStepper)!="number") {
CountStepper = -1;
}
if (typeof(LeadingZero)=="undefined") {
LeadingZero = true;
}
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0) {
CountActive = false;
}
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0) {
ddiff = new Date(dnow-dthen);
}
else {
ddiff = new Date(dthen-dnow);
}
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
</script>
I would use a simpler solution, using javascript Date object, sum miliseconds on a time counter then format the miliseconds as time.
some references :
Date object and functions : http://w3schools.com/jsref/jsref_obj_date.asp