Avoid an error from an undefined input-field in javascript [duplicate] - javascript

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 4 years ago.
I have an input-field in my form which sometimes gets displayed and sometimes not. When the input-field doesn´t get displayed and the form is about to get sent the script throws an error:
Cannot read property 'value' of undefined
I tried to catch that with this code:
if (typeof document.forms["add-new-job"].addjob_companyselect.value !== 'undefined') {
// do something
}
But the same error comes again on this line. How can I skip an undefined field?

var addNewJobElement = document.forms["add-new-job"].addjob_companyselect;
if (addNewJobElement && addNewJobElement.value) {
// do something
}

Related

Ignore javascript errors like in php [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 2 years ago.
I'm parsing some JSON with jQuery into array.
So there is a place where i'm assigning this:
$("#address_text").text(data['account']['address']['text']);
The problem is that sometimes i don't have this in the JSON and i've got error:
TypeError: null is not an object (evaluating 'data['account']['address']')
And the script is blocked under that line.
Is there anyway that i can ignore the error and assign nothing to #address_text ?
I search something like "#" sign in php. That no matter what is the error, just ignoring it.
First: If you have an error, fix the error not ignore it.
Second: Check if value exist before get the property
if (data && data['account'] && data['account']['address'] && data['account']['address']['text']) {
....
}
How about this?
if(data && data.account && data.account.address && data.account.address.text){
$("#address_text").text(data['account']['address']['text']);
}else{
$("#address_text").text("data is not valid");
}

why is my function giving an error message [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 2 years ago.
Hoping you good folk can help me with this one. I am trying to use a nav link to show a hidden div when clicked but I keep getting the error message
"Uncaught TypeError: Cannot read property 'display' of undefined
at HTMLAnchorElement."
this appears for the line of code = if (contentOneClick.style.display == "none") yet if i taker either the word "style" or the word "display" out of this code then i do not get the error message which has left me confused as i have watched content of this being used and working well for others. below is the whole function code. The display is set to "none" in the style code. I am new to web development so my apologies if this is a silly question.
`document.getElementById('content-one').addEventListener("click", function() {
var contentOneClick = document.getElementsByClassName(".content-one-container");
if (contentOneClick.style.display == "none")
{
console.log ("content is hidden");
}
else
{
console.log("content is visible");
}
}); `
The problem is that getElementsByClassName returns a nodeList, therefore, you should specify which element having class content-one-container you want to use:
var contentOneClick = document.getElementsByClassName("content-one-container")[0];
^ remove the .

Unable to stop repeating the div calling [duplicate]

This question already has answers here:
About jQuery append() and how to check if an element has been appended
(6 answers)
Closed 3 years ago.
I created one warning msg for warning the incorrect phone number. it is displaying even if the warning message is displayed. I want to check whether the error msg is displayed if no then display else dont want to display .
Here is my code.
jquery code
$('#mobile').val().length != 10
? (
$('#errorID')
?
$("#mobile").after('<div id="errorID" class="alert alert-danger">Please enter Valid mobile</div>')
: null
)
:
($("div").remove("#errorID"), mob = ($("#mobile").val()));
Use .length
$('#errorID').length > 0 there is a div even without > 0 it will return true as well
$('#errorID').length < 1 there is no div
While id should be unique you can directly use $("#errorID").remove()

Cloud function query returning null even though it should return a value [duplicate]

This question already has answers here:
Firebase Query Double Nested
(3 answers)
Closed 5 years ago.
This is the structure of the database,
Content {
"randomID" {
"randomID2" {
"text": "Hello World"
}
}
}
I'm trying to do a query in cloud functions, to check if text is equalTo a specific value but it always returns null
admin.database().ref('Content').orderByChild('text').equalTo("someText").once('value').then(snapshot => {
console.log(snapshot.val()); -> this returns null
})
I'm pretty sure the issue is that I have two random ID's there, also wildcards don't work for .once('value') like they do for onUpdate I believe. Or I'm doing something wrong as I'm quite new to Javascript.
admin.database().ref('Content').orderByChild('text')
should be changed to
admin.database().ref('Content/{randomID}/{randomID2}').orderByChild('text')
you can't skip the ids in the reference

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

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) {...}

Categories

Resources