JavaScript if else is not working properly - javascript

function food(){
var food_choice = document.getElementById('food-ch').value;
console.log(food_choice);
var food_arr = ['tuna','salmon','prawn','chicken'];
for(key in food_arr){
if(food_choice === food_arr[key]){
console.log(food_arr[key]);
$('#display-food').html("You have chosen "+food_arr[key]);
}
else{
$('#display-food').html("Please try again");
}
}
}
The above code is not working as I intended to. Whenever I was entering one of the items from the array, it went to else part.

function food(){
var food_choice = document.getElementById('food-ch').value;
console.log(food_choice);
var food_arr = ['tuna','salmon','prawn','chicken'];
if(food_arr.indexOf(food_choice) != -1){
$('#display-food').html("You have chosen" +food_choice);
} else {
$('#display-food').html("Please try again");
}
}

Related

window.location.href does not redirect

my functions are as follows
<a onClick="check_claims(this)" type="button" href="javascript:void(0)" redirurl="www.facebook.com" >Invite</a>
function check_claims(ele){
var select_claims = document.getElementById('select_claims').value;
if (select_claims == '1') {
var result = confirm("do you wish to continue?");
if (!result) {
check_email_address();
return;
}
}
check_email_address();
window.location.href = ele.getAttribute('redirurl');
}
function check_email_address(){
if ('{{broker.email_address}}' == 'None') {
console.log('this worked till here');
window.location.href = 'www.google.com';
}
}
I just added the second function check_email_address. the function gets called and the log gets printed but the windows.location.href of this function does not get evaluated and the page keeps redirecting to url mentioned in the window.location.href of first function.
Try using return false; at the end of the handler.
You can refer to this question for more details.
EDIT: Try this please.
function check_claims(ele){
var select_claims = document.getElementById('select_claims').value;
if (select_claims == '1') {
var result = confirm("do you wish to continue?");
if (!result) {
check_email_address();
return;
}
}
var email_check = check_email_address();
if (!email_check) window.location.href = ele.getAttribute('redirurl');
}
function check_email_address(){
if ('{{broker.email_address}}' == 'None') {
console.log('this worked till here');
window.location.href = 'www.google.com';
return true;
}
return false;
}

Javascript toggle button fails the if condition

Guys what am I doing wrong? On my pc this code: simple toggle shows me false === True when I call the function for the first time. Why it goes on else when status is false?
var status = false;
function toggleStatus() {
var message = "status:" + status;
if (status === false) {
message += "===FALSE";
status=true;
} else {
message += "===TRUE";
status=false;
}
alert(message);
}
It will be because there's already a window.status defined (which is a string, and used for the text in the browser status bar).
Because you're not protecting the global namespace from your code, the status variable is being defined as a global on the window object. Presumably the browser is going to stomp all over that.
This however should work https://jsfiddle.net/72rf8g8p/:
(function(){
var status = false;
function toggleStatus() {
var message = "status:" + status;
if (status === false) {
message += "===FALSE";
status=true;
} else {
message += "===TRUE";
status=false;
}
alert(message);
}
toggleStatus();
})();
You can also simply change the name of your variable to something else, such as myStatus:
var myStatus= false;
function toggleStatus() {
var message = "myStatus:" + myStatus;
if (myStatus=== false) {
message += "===FALSE";
myStatus=true;
} else {
message += "===TRUE";
myStatus=false;
}
alert(message);
}

if statement in jQuery not working in Chrome or Safari

I have the following code that is working in IE 8 but not in Chrome or Safari:
$(document).ready(function(){
$('.goRedIMG').on('click',function(event){
var ischecked = false;
var isOKtoSubmit = true;
var alertMessage = 'No tools have been selected';
var statusvar = '';
var transferstatusvar = '';
var action = $('#uTransaction option:selected').html();
$('.chkaction').each(function() { //loop through each checkbox
statusvar = $(this).closest('tr').children('.recordStatus').html();
transferstatusvar = $(this).closest('tr').children('.transferstat').html()
if($(this).prop('checked')) {
ischecked = true;
//alert(action);
// alert(statusvar);
// alert(transferstatusvar);
if (action == 'Recover'){
if (statusvar != 'OOS'){
// alert(statusvar);
isOKtoSubmit = false;
alertMessage = 'One or more records cannot be recoverd due to status not being OOS and Transfer Status not OK';
}
}
if(isOKtoSubmit && ischecked !== false && action !== '--Select One--'){
$('#toolActions').submit();
}else {
alert(alertMessage);
}
});
If a user chooses Recover and chooses a record that has a status that is in 'OOS' they are getting the alert message in Chrome that the record does not have the correct status. In IE if you choose the same record the alert message does not appear and that would be correct.
When I use your code like this:
var action = 'Recover';
var statusvar = 'OOS';
if (action == 'Recover') {
if (statusvar != 'OOS') {
alert('One or more records cannot be recoverd due to status not being OOS and Transfer Status not OK');
}
}
in both browsers it runs correctly. I think you have problem with your data.
In your original code try to use
alert(statusvar + ' - length:' + statusvar.length)
And check the character lenght of the variable. This way you can see if there is any funny character in your statusvar variable.

If Statement Not Acting As Expected

$(document).ready(function(){
logger();
});
function logger()
{
if(localStorage.getItem("status") === null)
{
$("#test").html("Not logged in.");
$("#buttonlogin").click(function(){
var ul = $("#userlogin").val();
var pl = $("#passlogin").val();
$.post("includes/logger.php", {type : "login", user : ul, pass : pl}, function(dlogin){
if(dlogin == 1)
{
$("#outlogin").html("Please enter a username.");
$("#userlogin").focus();
}
else if(dlogin == 2)
{
$("#outlogin").html("Please enter password.");
$("#passlogin").focus();
}
else if(dlogin == 3)
{
$("#outlogin").html("This username doesn't exist.");
$("#userlogin").focus();
}
else if(dlogin == 4)
{
$("#outlogin").html("This username and password don't match.");
$("#userlogin").focus();
}
else
{
localStorage.setItem("status", dlogin);
logger();
}
});
});
$("#buttonregister").click(function(){
var ur = $("#userregister").val();
var pr = $("#passregister").val();
var cpr = $("#confirmpassregister").val();
$.post("includes/logger.php", {type : "register", user : ur, pass : pr, cpass : cpr}, function(dregister){
if(dregister == 1)
{
$("#outregister").html("Please enter a username.");
$("#userregister").focus();
}
else if(dregister == 2)
{
$("#outregister").html("Please enter a password.");
$("#passregister").focus();
}
else if(deregister == 3)
{
$("#outregister").html("Please enter a confirm password.");
$("#cpassregister").focus();
}
else if(dregister == 4)
{
$("#outregister").html("Password and confirm password do not match.");
$("#passregister").focus();
}
else if(dregister == 5)
{
$("#outregister").html("This username is already taken.");
$("#userregister").focus();
}
else
{
localStorage.setItem("status", dregister);
logger();
}
});
});
}
else
{
$("#test").html("You are logged in.");
$("#buttonlogout").click(function(){
localStorage.removeItem("status");
logger();
});
}
}
The above code is meant to check whether or not a localStorage variable is in existence or not. If it is then only allow the log out button to be pressed. If is doesn't then let the two forms to work. Once it is done with either it is supposed to recheck if the variable is set and then do as I said above. However it ignores it when a user logs in and allows the forms to run. If you refresh however it works fine. I cannot for the life of me figure out why this is happening, and it is beginning to piss me off. Any help would be appreciated.
On your else statement, try adding:
$('#buttonlogin').unbind('click');
$('#buttonregister').unbind('click');
If I understand your problem correctly, what's happening is those events are registered when you first run $("#buttonlogin").click(function()....
It doesn't matter that you call logger() again and the if statement is false the second time around. If you want to disable these callbacks you have to do it explicitly.

How to get the values from Dropdown listbox and textfield ,then send it that values to webservices?

In this Code i try to get the value from text-field and dropdown listbox , I get values dynamically from user and send that value to webserices, In Given code get the value pass that values to webservice through javascript, but script didn't reponse to that code.. any one help me to fix this problem.
Here Code:
<body style=" "><script type="text/JavaScript" >
var xmlhttpuserid;
functionmyFunction() {
var checkid=new Array();
var userid = document.getElementById("userid").value;
for(var i=0;i<2;i++)
{
if(document.getElementById('domainid'+i).checked==true)
{
checkid[i]=document.getElementById('domainid'+i).value;
alert(checkid);
}
}
// var domainid = document.getElementById("").value;
//alert(userid);
var url= "../webservice/Passwordstation/ws_userauthpwdstation.jsp? userid="+userid+"&domain="+checkid;
alert(url);
xmlhttpduserid=GetXmlHttpObject();
if (xmlhttpduserid==null)
{
alert ("Your browser does not support Ajax HTTP");
return;
}
xmlhttpduserid.onreadystatechange=getuserid;
xmlhttpduserid.open("GET",url,true);
xmlhttpduserid.send(null);
}
function GetXmlHttpObject()
{
//alert("GetXmlHttpObject1");
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function getuserid()
{
if (xmlhttpduserid.readyState==4)
{
var text=xmlhttpduserid.responseText;
//alert(text);
text=text.replace(/^\s+|\s+$/g,"");
// alert("Text 2"+text);
if(text.match("SUCCESS"))
{
alert("Authenticate successfully");
window.location="accountmain.jsp";
}
else
{
alert("Please check your User id");
}
}
}
I hope this will help you .
You can get them in java script and pass them in the query as query parameter as already doing for user id .
// var domainid = document.getElementById("").value;
//alert(userid);
var textboxval = document.getElamentById("mytextbox").value;
var dropDown = document.getElementById("ddlViewBy");
var dropDownValue= dropDown.options[dropDown.selectedIndex].value;
var url= "../webservice/Passwordstation/ws_userauthpwdstation.jsp? userid="+userid+"&domain="+checkid&textboxvalue="+textboxval&selectedFromDropDown="+dropDownValue;

Categories

Resources