Comparing variable to empty jquery object in if condition [duplicate] - javascript

This question already has answers here:
How do you check if a selector matches something in jQuery? [duplicate]
(11 answers)
Closed 9 years ago.
In the jQuery accordion API, it says "If the accordion is collapsing, ui.newHeader and ui.newPanel will be empty jQuery objects."
How can I check if ui.newheader is an empty jQuery object? I've tried it like this:
if ($(ui.newHeader) == null)
{
...
}
,like this:
if (ui.newHeader == null)
{
...
}
and this:
if ($(ui.newHeader) == "")
{
...
}
So basically, this is a question about jquery/javascript syntax :) Thanks

What you want is to know if there is 0 element in the set. Do it like this :
if ($(ui.newHeader).length==0) {

if (!$(ui.newHeader).length)
or
if (!$(ui.newHeader)[0])

jQuery object is array like collection. So, it is empty means, it's length property is 0.
if(!$(ui.newHeader).length) {...}

Related

Whats a cleaner way to write this javascript if statement? [duplicate]

This question already has answers here:
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 1 year ago.
I've tried the format like
['mobile-number', 'name', 'about'].includes(stages)
But that doesn't seem to work.
if (stages.includes('mobile-number') || stages.includes('name') || stages.includes('about')) {
array.push('about-you')
}
Your looking for Array.prototype.some
stages.some(s => ['mobile-number', 'name', 'about'].includes(s))
This is the exact alternative of stages.includes('mobile-number') || stages.includes('name') || stages.includes('about')

How to use a variable in a Jquery selector [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I want to be able to use a variable inside a Jquery selector:
I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value
var myVariable;
//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
myVariable = 1;
}
else if ($('#myValue21').val() == "")
{
myVariable = 2;
}
else if ($('#myValue31').val() == "")
{
myVariable = 3;
}
//inserting into the first empty value, by using 'myVariable'
$('#myValue+myVariable+1').val("Hello World!"); //Doesn't work
The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you
Try
$('#myValue'+myVariable+'1').val("Hello World!");
You needed the extra quotes to terminate the strings.

Find and delete cookie with javascript [duplicate]

This question already has answers here:
How to delete a cookie?
(13 answers)
Closed 6 years ago.
How would like to with javascript find a cookie that starts with 'AzureAppProxyAccess' and delete that cookie? It always has a series of random numbers at the end of the name. It is in the same domain so I have access to it.
This is what I have tried with jquery but I would like just javascript.
for (cookie in $.cookie()) {
if(cookie.substring(0, 19) === "AzureAppProxyAccess") {
$.removeCookie(cookie);
}
};
You can use indexOf in strings.
cookie.indexOf('AzureAppProxyAccess') > 1 ? doSomething : somethingElse
Or, like it was said in one of the comments, use RegEx.

Is there any clean way to code a group of || testing to see if the value of a variable is in one of them? [duplicate]

This question already has answers here:
Javascript shorthand if
(5 answers)
Closed 9 years ago.
I have code like this:
if (view == 'delete' || view =='edit' || view == 'new') {}
Is there a more clean way that I can code this without using an external library. It just does not look too good to have all the || and maybe more later.
Use indexOf (won't work on ancient IE versions):
if (["delete", "edit", "new"].indexOf( view ) >= 0) { alert("ME");}
You can also use regular expression:
if (/^(delete|edit|new)$/.test(view)) {
// true
}

searching values in array javascript [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.

Categories

Resources