.focusout() only reading through first if() statement - javascript

Why is this not reading through both if statements even though they are both true?
HTML
<textarea name="test">
Focus out to test prompts
</textarea>
jQuery
var disableA = 1;
var disableB = 1;
$('textarea[name="test"]').focusout(function() {
if (disableA == 1) {
disableX();
}
if (disableB == 1) {
disableY();
}
});
function disableX() {
alert('A is disabled');
}
function disabledY() {
alert('B is disabled');
}
Right now it will call for disableX(); but not disableY()
jsFiddle: http://jsfiddle.net/nCQQm/

In your second function you have called it disabledY, whereas you are calling back disableY()?

You spelled disableY wrong you need to rename it to disabledY()

There is no problem with the if statements.
Running your code gives the error:
ReferenceError: disableY is not defined
You have named the second function disabledY, and then you try to call disableY.

Related

How to match anything between < & > in regex?

I'm trying to match anything that lies between < and >, and nothing seems to be working.
My current code is:
var regex = /\<(.*?)\>/
var targeting = $('#auto-expand').val //A text area
function validateText(field)
{
if (regex.test(field) == true)
{
alert(field.match(regex))
}
else
{
alert("fail")
}
}
It keeps returning fail, not sure why.
Any help would be so great! :)
It's not clear from your question how you are calling the validateText function. But it looks like are trying to set targeting outside the function, which means you are probably setting it before there's text in the box.
Below I change val to val() to call the function and looked up the value when the function runs rather than before. The regex itself works fine (keeping this in mind)
var regex = /<(.*?)>/
function validateText() {
var targeting = $('#auto-expand').val() //A text area
if (regex.test(targeting) == true) {
alert(targeting.match(regex))
} else {
alert("fail")
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="auto-expand"></textarea>
<button onclick=validateText()>Test</button>

Javascript input onkeypress function do not work

I put the script in head tags
function check(e, regexp) {
if(navigator.userAgent.indexOf('Gecko') != -1) {
charCode = e.which;
} else {
charCode = e.keyCode;
}
if(charCode > 31) {
znak = String.fromCharCode(charCode);
return regexp.test(znak);
}
}
When I use this function in input
<input id='pole2' name=nip style='WIDTH: 300px;' onkeypress='return check(event, /[0-9-]/i);'>
it works great with HTML.
But when I create in javascript input with id='odbiorca' and if I try to use onkeypress with the same code
document.getElementById("odbiorca").onkeypress = function() {myfunction()};
function myFunction(){
return check(event, /[0-9- ]/i);
}
It does not want to work. What is wrong?
The last line of the script should be written like this:
document.getElementById("odbiorca").onkeypress = function(event) {return check(event, /[0-9- ]/i);}
As you see the typo was not a main problem. Keep this solution for others.
Thanks to Merijn for suggestion with browser console.
The problem is that you mispelled myFunction:
document.getElementById("odbiorca").onkeypress = function() {myfunction()};
function myFunction(){
return check(event, /[0-9- ]/i);
}
myFunction is with a capital F but you call it with a small f (myfunction instead of myFunction) beside that it should work!
EDIT: try to check your browser console first next time. You could have seen the very clear error:
Uncaught ReferenceError: myfunction is not defined
at HTMLInputElement.document.getElementById.onkeypress

My onClick button will not execute the JavaScript

This is my javascipt function: I am trying to swap an image, en be able to swap it back again with a button. But for some reason, it won't execute the script.
Javascript
function changeImage() {
if (document.GetElementById('flashlight').src == "img\flashlight.png") {
image.src="img\flashlightON.png";
} else (document.GetElementById('flashlight').src == "img\flashlightON.png"); {
image.src="img\flashlight.png";
}
}
and here is my HTML:
<button type="button" onClick="changeImage()" class="classname"> </button>
Javascript is case sensitive. In your case it is getElementById and not GetElementById. You placed your alert probably as the first thing in your function and therefore that part worked. Javascript is not precompiled so you just get the syntax error at the moment the line is trying to be executed.
You can use firebug to debug javascript.
You have syntax errors. Use this:
function changeImage() {
var image = document.getElementById('flashlight');
if (image.src == "img/flashlight.png") {
image.src = "img/flashlightON.png";
} else {
image.src = "img/flashlight.png";
}
}
Javascript is case sensitive so the first mistake is in document.GetElementById. It should have been document.getElementById in order to change the src you can bind the onclick to the button in the js part itself like:
theButton.onclick = function pictureChange()
The complete working example is in this fiddle
Try this... You'll need to add a src to your img in html
function swapImage() {
if (document.getElementById("flashlight").src.endsWith("Images/Image1.png") == true)//Comparison
{
document.getElementById("flashlight").src = "Images/Image2.png";//assignment
}
else if (document.getElementById("flashlight").src.endsWith("Images/Image2.png") == true)
{
document.getElementById("flashlight").src = "Images/Image1.png";
}
}

Access variable on other function

In my below jQuery code, I can't get correct length of inputString element, element's length changing on lookup function and I can't use global variable.
$('#ckekeKala').live("click" ,function(){
var len=$('#inputString').text().length;
alert(len);
});
function lookup(inputString) {
if(inputString.length != 0) {
$('[id^="lbl"]').attr("disabled", true);
}
});
}
} // lookup
HTML code:
<input style='width: 128px;' name='nameKala' id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
Extra braces in lookup function
function lookup(inputString) {
if(inputString.length != 0) {
$('[id^="lbl"]').attr("disabled", true);
}
}); <--- Remove this
} <--- Remove this
} // lookup
Just a thought, you could really easy this whole process up like so:
HTML
<input id="inputString" name="nameKala" type="text" style="width:128px" />
Script
// keep in mind, that depending on ur use and jQuery version, .live can be replaced with .bind or .on
$("#inputString").live("keyup", function(e) {
if ($(this).length > 0) {
$('[id^="lbl"]').attr("disabled", true);
};
})
.live("blur", function(e) {
// do fill work
});
$('#ckekeKala').live("click" ,function(){
var len=$('#inputString').val().length;
alert(len);
});
Example
Beside any syntax error that you can see in other answer and post. It would have been easy if you have put more code. But assuming that all other code that you haven't mention are correct and fixing this syntax error of
});
I think your lookup does not have access outside jQuery block. so what you can do is change it to make like this
lookup = function(inputString) {
if (inputString.length != 0) {
$('[id^="lbl"]').attr("disabled", true);
}
}

comparing innerHTML value with a string variable in javascript

function Open() {
var cc = document.getElementById('FName');
if ('Newfile.rtf' == cc.innerHTML)
{
alert("New File");
} //close If NewFile.rtf
else {
alert("Not new file");
}
}//close Open()
Here I have string "NewFile.rtf" in a element with id="FName" on the page. When the FName contains "Newfile.rtf" in it it stills goes to the else part of the function instead of going to if part. I tried different ways to write the compare statement in the if condition, no luck . Appreciate the help if anyone can help figure out this.
Thank you.
The simplest explanation is that your cc.innerHTML call is not returning what you think it is returning. Why don't you console.log or debug.
add something like
var innerhtml = cc.innerHTML;
console.log("innerHTML = " + innerhtml) // wont work in IE.
before the if statement.
Try using regular expressions to find your filename, also check if the text you are searching is not into another DOM element, elimate left and right spaces, you should use Google Chrome for debuging the Javascript code:
var html = document.getElementById('FName').innerHTML;
if( html.search("Newfile.rtf") != -1) { /*found*/ }
else { /*not found*/ }
but what's the type of this element? if it's about an input text type .. you can't use innerHTML but you'll use value then.
Use innerText to get that
function Open() {
var cc = document.getElementById('FName');
if ('Newfile.rtf' == cc.innerText)
{
alert("New File");
} //close If NewFile.rtf
else {
//enter code here
alert("Not new file");
}
}

Categories