I am having issue waiting on a DOM element to load. I am trying to get the value in an HTML input. Here are the 2 methods that I used but they both did not work:
Option 1):
$(document).ready(function() {
var inputVal = $(".class-name tag-label");
if (inputVal != undefined && inputVal.innerText != undefined && inputVal.innerText != ""){
//do something
}
else{
//not to do something
}
});
Option 2):
$(".class-name tag-label").ready(function() {
var inputVal = $(".class-name tag-label");
if (inputVal != undefined && inputVal.innerText != undefined && inputVal.innerText != ""){
//do something
}
else{
//not to do something
}
});
However, after the page is loaded, and I do $(".class-name tag-label") and it's returning the proper element with a value in the console. Anything I did incorrectly?
Thanks for your comments. I realized that document was undefined, and was actually missing windows.load
window.addEventListener('load', (event) => {
var inputVal = $(".class-name tag-label");
if (inputVal != undefined && inputVal.innerText != undefined && inputVal.innerText != ""){
//do something
}
else{
//not to do something
}
});
This works for me ^
Related
I try to display Login Screen in the spesific URL ('ads/audience-insights') If the user is not logged in. but no luck.
Can you correct my code? what's wrong?
if (window.location.href.indexOf('ads/audience-insights') != -1) {
chrome.storage.local.get("cnt9", function(result) {
if (result.cnt9 == null || result.cnt9 == -1 || result.cnt9 >= 40) {
// show login screen
showLoginScreen();
}
});
}
This code == user Not logged in yet..
result.cnt9 == null || result.cnt9 == -1 || result.cnt9 >= 40
You don't have to use chrome.storage or the callback function.
if (typeof(Storage) !== "undefined") {
var a = localStorage.cnt9;
if (a == null || a == -1 || a >= 40)
alert("Not Logged In!");
}
You also do not have to check if the user is on a specific page as the code will not execute if they are not on said page in the first place.
I dont want to do the if statement when on pageAge OR ON pageMore. When i go to pageAge it works it doesn't execute the script but when I go to pageMore it does. I'm not sure what operator to use in this situation. When I put pageMore before the || it works on that page but not on the othe one.
if ( top.location.pathname != pageAge || pageMore) {
//if not verified go to connect
$("body").css("display", "none");
if (age === null && top.location.pathname != pageConnect) {
window.location.href = pageConnect;
}
//if to young go to age page while cookie is found (1day)
if (age == toYoung) {
window.location.href = pageAge;
}
//if already verified go to like page.
if (age == legal && top.location.pathname === pageConnect) {
window.location.href = pageLike;
}
}
It should be:
if ( top.location.pathname != pageAge && top.location.pathname != pageMore)
I am trying to get it where, if there is value in the input, go ahead and do your thing but if there isn't, don't do anything. I keep getting getting it where, if there is nothing in the input, a failure message occurs but only if I hit the enter key
jsfiddle.net/BBaughn/8ovrmhgp click the space in the lower right corner, then press enter. It shouldn't pop up, because the input is not focused.
login/failure jQuery:
$(document).ready(function(){
$('.btn1').on('click', function(){
var login = [marion, 'FATHER1'];
var marion = $('#logging').val();
marion = marion.toUpperCase();
if (marion !== 'FATHER1' && $('#logging').val()) {
alert('Login Failed');
} else if (marion === 'FATHER1' && $('#logging').val()) {
$('.notify').css('margin-top', '0');
$('#logging').val('');
}
});
$('.exit').on('click', function(){
$('.notify').slideUp('slow');
});
});
if you just want to check if an input is empty, I'm guessing #logging is the input:
$('.btn1').on('click', function(){
var marion = $('#logging').val();
if (marion == '') {
//this means the input value was empty
}
});
if the length is equal zero, that means the field is empty. It works after adding length === 0 check, please try this,
if (e.which == 13 && $('.btn1').val().length === 0) {
e.preventDefault();
} else if (e.which == 13 && $('.btn1').focus()) {
$('.btn1').click();
}
Ok, I think I get what you mean now. You're checking the entire document for keypresses as is, you need to check the input only. I think this is a good solution to do that:
$('.login input').keypress(function (e) {
if (e.which == 13 && !$('#logging').val()) {
e.PreventDefault();
} else if (e.which == 13 && $('.btn1').focus()) {
$('.btn1').click();
}
});
Working Fiddle here
$(document).ready(function(){
$('.btn1').on('click', function(){
var login = [marion, 'FATHER1'];
var marion = $('#logging').val();
marion = marion.toUpperCase();
if (marion !== 'FATHER1' && $('#logging').val()) {
alert('Login Failed');
} else if (marion === 'FATHER1' && $('#logging').val()) {
$('.notify').css('margin-top', '0');
$('#logging').val('');
}
});
$('.exit').on('click', function(){
$('.notify').slideUp('slow');
});
});
I didn't realize that my if statement was if the input wasn't detecting the right login, so it didn't matter if it was out of focus. Now it says "if it's not FATHER1 and there is value in the input only, then send this alert"
I am using some elements (input, select, textarea etc.) to send data from client inputs to server via XHR(ajax).
While processing data and in sending process I give informations to user with modal dialog and block his screen.
After sent success and everythings done right, give "success info" and unblock his screen.
Also I must clean (reset) input fileds. I didn't use form, so I couldn't use reset().
I try to doing this with the following JavaScript:
function hedefiSifirla(anacElementID) {
var anacElement = (nY(anacElementID) ? nY(anacElementID) : anacElementID);
for (xi, xiLen = anacElement.children.length; xi < xiLen; xi++) {
var elm = anacElement.children[xi];
if (elm.hasChildNodes()) {
hedefiSifirla(elm)
}
if ((elm.tagName == 'input' && elm.type != 'checkbox') || elm.tagName == 'textarea') {
alert(elm.id);
elm.value = '';
}
if (elm.tagName == 'input' && elm.type == 'checkbox') {
elm.checked = false;
alert(elm.id);
}
if (elm.tagName == 'select') {
elm.options.length = 0;
alert(elm.id);
}
}
}
This didn't work with any error log. Where did i go wrong?
I want to check if my application's buttons are pressed or not. The error I face is, even when the buttons are clicked all the alerts display. Attached is the code snippet, the variables are set by click of buttons.
I want the alert not to display if any of the values are selected,
var condition ;
var clickable; // GLOBAL VARIABLES
function clickMe1()
{
clickable = "Sell";
}
function clickMe2()
{
clickable = "Rent";
}
function condition1()
{
condition="Excellent"
}
function condition2()
{
condition="Good"
}
function condition3()
{
condition="Fair"
}
function condition4()
{
condition="New"
}
function display()
{
if (condition != "Excellent"||"New"||"Fair"||"Good")
{
alert( " Please enter the condition ");
}
if (clickable != "Sell"||"Rent")
{
alert("Please enter the Sell");
}
if (costSell === '')
{
alert("Please select a Price ");
}
if ((condition === "Excellent"||"New"||"Fair"||"Good") && (clickable === "Selling"||"leasing")&&(!isNaN(costSell)))
{
// Do Something
},
error: function(data){
console.log("not added");
}
});
}
else
{
alert(" price is not a number");
}
}
I also tried:
if(condition !='Excellent'|| condition!='New' || condition!='Fair'|| condition!='Good')
{
alert( " Please enter the condition ");
}
if (clickable !='Sell'||'Rent' )
{
alert("Please enter the Sell ");
if(condition !='Excellent'|| condition!='New' || condition!='Fair'|| condition!='Good')
should be
if (condition != 'Excellent' && condition != 'New' && condition != 'Fair' && condition != 'Good')
because your version triggers if the condition is any one of Excellent, New, Fair, or Good. The corrected line triggers when the condition is not one of those.
And
if (clickable !='Sell'||'Rent' )
should be
if (clickable !='Sell' && clickable !='Rent' )
because you can't make that shortcut of only using clickable once.
condition !="Excellent"||"New"||"Fair"||"Good"
Conditions like this are your problem.
condition !="Excellent" && condition != "New" ...
^The solution
Your problem is you're testing multiple conditions without repeating the left-hand operand.
For example:
condition !="Excellent"||"New"||"Fair"||"Good"
It should be this:
condition != "Excellent" || condition != "New" || condition != "Fair" || condition !="Good"