I couldn't find out why my code doesn't work correctly
I have this function:
function checkIt(input_name, input_value){
if (input_name === 'firstname') {
y = regs[0];
} else if (input_name === 'lastname') {
y = regs[1];
} else if (input_name === 'phone') {
y = regs[2];
} else if (input_name === 'mail') {
y = regs[3];
} else {
y = regs[4];
}
z = input_value.match(y1);
if (z !== null) {
rez = true;
} else {
rez = false;
}
return rez;
}
and then im doing this:
$(document).on("blur", "input[name=firstname]", function(){
p = $(this).prop("name");
q = $(this).val();
r = checkIt(p, q);
if (r) {
// something
} else {
// something
}
});
and my problem is that my code doesn't work on first blur. it's starting executing on second blur and does everything perfecty... :/
try using on("blur", "input", function) without parameter name, because do u have the var p with prop of this input.
$(document).on("blur", "input", function(){
p = $(this).prop("name");
q = $(this).val();
r = checkIt(p, q);
if (r) {
// something
} else {
// something
}
});
Related
I'm trying to make an interactive mall map. JS code works correctly on hover. But I need to add on click as well. That is, to work the same way when hovering and clicking on the menu.
Here is the JS code:
//map
var id = 0;
$(' .scheme-about__item .menu__link').mouseover(function(e) {
id = $(this).attr('data-id');
if(isMobile()) {
$('.area-m-' + id).addClass('hover');
} else {
$('.area-' + id).addClass('hover');
}
console.log($('.area-' + id));
})
.mouseout(function(e) {
if(isMobile()) {
$('.area-m-' + id).removeClass('hover');
} else {
$('.area-' + id).removeClass('hover');
}
});
function isMobile() {
var res = navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|Windows Phone|PlayBook|BB10|Nokia/i);
if (res != null)
return true;
else
return false;
}
You can try this one.
//map
let id = 0;
let menuLink = document.querySelector('.scheme-about__item .menu__link');
menuLink.addEventListener('click',mapFunction);
menuLink.addEventListener('mouseover',mapFunction);
menuLink.addEventListener('mouseout',mapFunction);
function mapFunction(e){
id = e.target.getAttribute('data-id');
if(isMobile()) {
document.querySelector('.area-m-' + id).classList.add('hover');
} else {
document.querySelector('.area-' + id).classList.add('hover');
}
console.log(document.querySelector('.area-' + id)
}
function isMobile() {
var res = navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|Windows Phone|PlayBook|BB10|Nokia/i);
if (res != null)
return true;
else
return false;
}
I have made this function into a JS file...
function getColors(isPick, isForecolor)
{
var chosenFunction = 'getColor(' + isPick + ', ' + isForecolor + ')';
csInterface.evalScript(chosenFunction, function(result)
{
if(result !== 'undefined')
{
if (isForecolor == true){
foregroundHexColor = result;
// etc...
}
else
{
backgroundHexColor = result;
//etc..
};
};
});
};
which get a hexadecimal color value from this function from a JSX file.
function getColor(isPick, isForecolor)
{
var color_PickerCase;
var decimal_Color;
var hexadecimal_Color;
if (isForecolor == true)
{
color_PickerCase = app.foregroundColor.rgb.hexValue;
}
else
{
color_PickerCase = app.backgroundColor.rgb.hexValue;
};
if (isPick == true)
{
if (app.showColorPicker(isForecolor)){
decimal_Color = color_PickerCase;
hexadecimal_Color = decimal_Color.toString(16);
}
else
{
return;
};
}
else
{
decimal_Color = color_PickerCase;
hexadecimal_Color = decimal_Color.toString(16);
};
return hexadecimal_Color;
};
In some way it works, but for some reason I have to do the same thing two times so to get the value!!! Any idea why is this happening?
Thank you for your time!!!
UPDATE: A correction, it works only at first click. Then needs to clicked two times so to get the value!!!
Well, here is the solution...
function getColor(isPick, isForecolor)
{
var color_PickerCase;
var decimal_Color;
var hexadecimal_Color;
if (isPick === true && app.showColorPicker(isForecolor) === false)
{
return;
}
if (isForecolor === true)
{
color_PickerCase = app.foregroundColor.rgb.hexValue;
}
else
{
color_PickerCase = app.backgroundColor.rgb.hexValue;
}
decimal_Color = color_PickerCase;
hexadecimal_Color = decimal_Color.toString(16);
return hexadecimal_Color;
};
As joojaa from graphicdesign said, I was asking for the color before picking it and I was getting the color form the last time!!!
I have JavaScript calculator wherein I have defined two arrays as follows:
var degInc, degArr = [];
var radInc, radArr = [];
var PI = Math.PI;
var radStart = (-91*PI/2), radEnd = (91*PI/2);
for (degInc = -8190; degInc <= 8190; degInc+=180) {
degArr.push(degInc);
}
for (radInc = radStart; radInc <= radEnd; radInc+=PI) {
var radIncFixed = radInc.toFixed(8);
radArr.push(radIncFixed);
}
to be used in conjunction with the tangent function (below) so as to display a value of Undefined in an input (HTML below) should the user attempt to take the tangent of these values (I have included other relavent function as well):
Input -
<INPUT NAME="display" ID="disp" VALUE="0" SIZE="28" MAXLENGTH="25"/>
Functions -
function tan(form) {
form.display.value = trigPrecision(Math.tan(form.display.value));
}
function tanDeg(form) {
form.display.value = trigPrecision(Math.tan(radians(form)));
}
function radians(form) {
return form.display.value * Math.PI / 180;
}
with jQuery -
$("#button-tan").click(function(){
if (checkNum(this.form.display.value)) {
if($("#button-mode").val() === 'DEG'){
tan(this.form); // INSERT OTHER 'if' STATEMENT HERE FOR RAD ARRAY
}
else{
tanDeg(this.form); // INSERT OTHER 'if' STATEMENT HERE FOR DEG ARRAY
}
}
});
I would like to incorporate an array check within the .click function such that if the user input is contained in the array (degArr or radArr depending on the mode), the calculator returns Undefined. Now, I know how to display Undefined in the input display ($('#disp').val('Undefined')), but I cannot figure out how to configure an if statement that checks the relevant array. Is there a way to do so within the #button-tan function where I have commented?
Loop through the arrays on click and set a variable if you find a matched value.
You can do something like this:
$("#button-tan").click(function(e) {
e.preventDefault();
var userInput = $('#disp').val();
var buttonMode = $('#button-mode').val();
var displayVal = '';
if (buttonMode === 'DEG') {
var radFound = false;
radArr.forEach(function(item) { // changed from degArr
if (item === userInput) {
radFound = true;
}
if (radFound) {
displayVal = 'undefined';
} else {
tan(this.form);
}
});
} else {
var degFound = false;
degArr.forEach(function(item) {
if (item === userInput) {
degFound = true;
}
if (degFound) {
displayVal = 'undefined';
} else {
tanDeg(this.form);
}
});
}
});
You could create a simple object of a Calculator class, which keeps a reference to these arrays, and use like this. I changed some methods to receive the input as parameter rather than form.
$(function () {
function Calculator()
{
var degInc;
this.degArr = [];
var radInc;
this.radArr = [];
var PI = Math.PI;
var radStart = (-91*PI/2);
var radEnd = (91*PI/2);
for (degInc = -8190; degInc <= 8190; degInc+=180) {
this.degArr.push(degInc);
}
for (radInc = radStart; radInc <= radEnd; radInc+=PI) {
var radIncFixed = radInc.toFixed(8);
this.radArr.push(radIncFixed);
}
}
var calc = new Calculator();
function tan(input) {
alert("tan called");
var value = Math.tan(input.value);
alert("tan called. value: " + value);
input.value = value;
}
function tanDeg(input) {
alert("tanDeg called");
var value = Math.tan(radians(input));
alert("tanDeg called. value: " + value);
input.value = value;
}
function radians(input) {
alert("radians called");
var value = input.value * Math.PI / 180;
alert("radians called. value: " + value);
return value;
}
$("#button-tan").click(function(){
alert (calc.degArr);
alert (calc.radArr);
var displayInput = $("#disp");
alert("user input: " + displayInput.val());
if (!isNaN(displayInput.val()))
{
if($("#button-mode").val() === 'DEG')
{
if (calc.radArr.indexOf(displayInput.val()) > -1)
{
alert("user input is in radArr");
}
else
{
alert("user input IS NOT in radArr");
tan(displayInput);
}
}
else
{
if (calc.degArr.indexOf(displayInput.val()) > -1)
{
alert("user input is in degArr");
}
else {
alert("user input IS NOT in degArr");
tan(displayInput);
}
}
}
else
alert("Not a number in input");
});
});
If you wanna do some tests, I created a JSFiddle demo here. Type -8190 in the first input, then click the button. It's gonna be inside the array. Then try typing "DEG" in the second input and clicking again, you'll notice code will check against another array (due to IFs). I couldn't make your auxiliar functions to calculate a value, but I think this helps you with your initial problem.
indexOf should work...
$("#button-tan").click(function(){
if (checkNum(this.form.display.value)) {
if($("#button-mode").val() === 'DEG'){
if (radArr.indexOf(Number(this.form)) > -1) {
$('#disp').val('Undefined');
} else {
tan(this.form);
}
}
else{
if (degArr.indexOf(Number(this.form)) > -1) {
$('#disp').val('Undefined');
} else {
tanDeg(this.form);
}
}
}
});
I have JavaScript code which displays a multi coffee value.
If I am trying to display a single value coffee, its not going inside the else if.
I modified existing code but getting an undefined error.
Can you tell me how to fix it?
var multCoffees = false;
var singleCoffee = false;
if (Coffees.length > 1) {
multCoffees = true;
}
if (Coffees.length > 1) {
singleCoffee = true;
}
if (apptTimeCell) {
apptTimeHTML = MyDay.dish(allData, multCoffees, singleCoffee);
apptTimeCell.innerHTML = apptTimeHTML;
} else {
apptTimeCell = Util.cep("span", {
className: "appt-time"
});
patientRowTD.insertBefore(apptTimeCell, patCell);
}
dish: function (allData, multCoffees, singleCoffee) {
if (multCoffees) {
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.MNEMONIC, "</span>");
console.log("multiCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else if (singleCoffee) {
console.log("inside if" + allData.PROVIDER_MNEMONIC);
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.PROVIDER_MNEMONIC, "</span>");
console.log("singleCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else {
return allData.APPT_TIME_DISPLAY;
}
},
Working code:
var multCoffees = false;
if (Coffees.length > 1) {
multCoffees = true;
}
if (apptTimeCell) {
apptTimeHTML = MyDay.dish(allData, multCoffees);
apptTimeCell.innerHTML = apptTimeHTML;
} else {
apptTimeCell = Util.cep("span", {
className: "appt-time"
});
patientRowTD.insertBefore(apptTimeCell, patCell);
}
dish: function (allData, multCoffees) {
if (multCoffees) {
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.MNEMONIC, "</span>");
console.log("multiCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else {
return allData.APPT_TIME_DISPLAY;
}
},
Suppose Coffees.length is 2. You do this...
if (Coffees.length > 1) {
multCoffees = true;
}
...and 2 > 1, so now multCoffees is true, but then you do this, which checks the same thing...
if (Coffees.length > 1) {
singleCoffee = true;
}
and, since 2 > 1 still, now BOTH multCoffees AND singleCoffee are true. So when you try to do
if (multCoffees) {
...
} else if (singleCoffee) {
...
}
the first if branch is true, so it is executed, and the else branch is thus ignored (despite also being true). You probably meant to instead start with
if (Coffees.length == 1) {
singleCoffee = true;
} else if (Coffees.length > 1) {
multCoffees = true;
}
replace your first two ifs with this block:
if (Coffees.length == 1) {
singleCoffee = true;
}
else if(Coffees.length > 1){
multCoffees = true;
}
and then try it again!
I am having two pages. The next button in first pages brings the second page. The focus in second page is always moving down. So I need to use scroll bar to bring the bring the cursor top. I want to bring the focus to the top. my focus-handler.js is as follows:
var lastFocusedControlId = "";
function focusHandler(e) {
document.activeElement = e.originalTarget;
}
function appInit() {
if (typeof (window.addEventListener) !== "undefined") {
window.addEventListener("focus", focusHandler, true);
}
setFirstControl()
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}
function pageLoadingHandler(sender, args) {
lastFocusedControlId = typeof (document.activeElement) === "undefined"
? "" : document.activeElement.id;
}
function focusControl(targetControl) {
if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
var focusTarget = targetControl;
if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) {
oldContentEditableSetting = focusTarget.contentEditable;
focusTarget.contentEditable = false;
}
else {
focusTarget = null;
}
try {
targetControl.focus();
if (focusTarget) {
focusTarget.contentEditable = oldContentEditableSetting;
}
}
catch (err) { }
}
else {
targetControl.focus();
}
}
function pageLoadedHandler(sender, args) {
if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
var newFocused = $get(lastFocusedControlId);
if (newFocused) {
focusControl(newFocused);
}
}
}
function setFirstControl() {
var bFound = false;
// for each form
for (f = 0; f < document.forms.length; f++) {
// for each element in each form
for (i = 0; i < document.forms[f].length; i++) {
// if it's not a hidden element
if (document.forms[f][i].type != "hidden") {
// and it's not disabled
if (document.forms[f][i].disabled != true) {
try {
// set the focus to it
document.forms[f][i].focus();
var bFound = true;
}
catch (er) {
}
}
}
// if found in this element, stop looking
if (bFound == true)
break;
}
// if found in this form, stop looking
if (bFound == true)
break;
}
}
Sys.Application.add_init(appInit);