JavaScript - check length value [duplicate] - javascript

This question already has answers here:
JavaScript get element by name [duplicate]
(8 answers)
Closed 7 years ago.
I would like to create a form, in this form I have to validate if the value is long enough.... More than 1 character.
My actual code looks like that:
function validateForm(){
alert("Form ok");
if(document.getElementsByName("firstname").value.length >1){
alert("if");
}
else{
document.getElementById("nameValidation").innerHTML= "* You must enter a first name";
alert("else");
}
I am struggling to understand why it doesn't work....
Many thanks.

You're using document.getElementsByName which returns NodeList. So, you should use it like this:
document.getElementsByName("firstname")[0].value.length > 1

getElementsByName returns array (or array-like object) of elements. You can access each element by index, e.g.:
document.getElementsByName("firstname")[0].value.length

Related

"in" keyword not working properly when used with object.value in node [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 1 year ago.
I need to execute some code. But my code is not working properly. I have made an simple example to demonstrate the malfunction.
When I run
console.log("val" in Object.values({key:"val"})); //returns false
It gives me false. But if I run
console.log(Object.values({key:"val"}))
outputs => ['val']
I don't understand if it is supposed to work like this. If yes. Why ?
Thanks in Advance.....:)
MDN says "the in operator returns true if the specified property is in the specified object or its prototype chain.", Object.values returns an array. To check if an item exists in an array, use the Array.includes method.
console.log(
Object.values({key:"val"}).includes("val")
); // Returns true

What is the Javascript equivalent of writing 'If not" [duplicate]

This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 6 years ago.
Python programmer here.
I don't know how to write this. I tried using 'if !in' and '!if in', but I don't know how. Tried to Google it but got no results.
The correct syntax is
if(!condition){
expression();
}
Note that you need parenthesis around the condition.
#plalx wants a formal definition, and here you go:
IfStatement:
if(Expression) Statement else Statement
if(Expression) Statement
In case of any ambiguity the else would be matched with the nearest if.
If you have some value:
var excludeMe = "something unwanted";
Then you can use the following if statement:
if(myTestCase !== excludeMe) { //do something...}
Keep in mind that != does not check type and !== does check type. So, 1 != "1" is false and 1 !== "1" is true.

JavaScript AnchorTag.text returning undefined [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 6 years ago.
I have to take school names from this page: http://www.australianschoolsdirectory.com.au/search-result.php for a work and I do not want to do it manually so I wrote a script and put it into the console:
var schoolNames = document.getElementsByClassName( "listing-header" );
console.log( schoolNames[0].getElementsByTagName( 'a' ).text );
This code returns undefined but I expect to get the name of the first school.
How to make it properly if I think that this is done properly haha?
Thank you!
The problem is that you are calling .text() on an array of results. The method that you used getElementsByTagName() returns an array. So when you want to get the text of such an element, say the first one, you'd use something like ths:
console.log( schoolNames[0].getElementsByTagName( 'a' )[0].text );
Just like you have done in the first part of your code after getting an array of all elements which had a class name var schoolNames = document.getElementsByClassName( "listing-header" ); and schoolNames[0]

Object length in typescript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
is there any way in typescript by which I can get length of an object:
Something like this:
say I have an object:
public customer:any={
"name":"Bhushan",
"eid":"879546",
"dept":"IT"
}
Now I am trying to get its length in typescript.
ie. when I am doing customer.length(), I should be able to get value 3 as it has 3 elements.
I tried Object.getOwnPropertyNames(customer.value) but its returning 2 whereas I have 3 elements in my object.
any inputs?
You could try the following:
Object.keys(customer).length
Object.keys(this.customer).length

check if a string is part of an array - javascript [duplicate]

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to be able to see if "megan" is a part of the array people. Though, i can only do this by using people[2].
Here is my code:
var people = new Array("jack","ian");
document.write(people[0]);
people.push("megan");
document.write("<br />");
Use the indexOf method of the array:
if(people.indexOf("megan") > -1) {
//do stuff
} else {
//not in array
}
If the string is in the array, 0 is returned. if not, -1 is returned.

Categories