Unable to stop repeating the div calling [duplicate] - javascript

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()

Related

Remove row if value equal to [duplicate]

This question already has answers here:
Looping through array and removing items, without breaking for loop
(17 answers)
Remove items from array with splice in for loop [duplicate]
(5 answers)
Closed 2 years ago.
I am trying to remove all the rows that don't have the value '2A' or '2B' but whenever I execute the code it deletes every row except the first one.
Here is my bit of code :
function supprimerColonnesInutiles() {
for(i = 1; i < data.length; i++) {
if(data[i][13] === '2019-09-30'){
data.splice(i);
}
if(data[i][2] !== '2A'){
data.splice(i);
}
}
}
This code works perfectly to delete every row with the 2019 date but fails to do what I expect it to do to delete rows that contain 2A as a value (i'd like to keep 2B values as well).
here is the csv file that serves as my array, as you can see the second column contains 2As and 2Bs but also number from 1 to 100.
This is what I get if I execute the code enter image description here
and here is my csv/array enter image description here
My question is what could be causing this issue and how can I fix it ?

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 .

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

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
}

Can't compare two strings in JavaScript [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 6 years ago.
I have the following function, which should return different values based on the head's text:
function getPage(){
var page;
var headText = $.trim($('h1').text());
if(headText=="Page 1"){
page= 'a';
}else if(headText=="Page 2"){
page= 'b';
}else if(headText=="Page 3"){
page ='c';
}else if(headText=="Page 4"){
page= 'd';
}else{
page= 'ERROR';
}
return page;
}
Unfortunately, the function only returns "ERROR". I've tried it without the trim method. I've tried it by writing Page 1 - Page 4 into variables of their own and comparing 2 variables in the if() section. Nothing works. Where is my error?
(I am certain that the text of the < h1>- Element is "Page 1", "Page 2", "Page 3" or "Page 4")
First, console.log(headText); to see what you're actually getting.
You may be getting the text of the h1's children added onto the text of the h1 - see http://api.jquery.com/text/, specifically
Get the combined text contents of each element in the set of matched elements, including their descendants. [emphasis mine]
If that's the case, see this question and answer for a solution.
Your code works fine if there is only one h1 tag on your page. If there are multiple h1 tags then their text will be concatenated in headText.
You should uniquely identify your h1 tag with an id attribute to get only that tag's text.

how to get value by class name using javascript [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 7 years ago.
Sorry, it's basic one but I trying to search on google anyways but still not get success.
I want get value of this
<input type='hidden' class='hid_id' value='1' />
Using Javascript I want alert value 1
I trying this
var id = document.getElementsByClassName("hid_id");
alert (id);
But it's alert [object HTMLInputElement]
please help me now.
getElementsByClassName() returns an array so you have to access first element (if there is any). Then try accessing value property:
var id = document.getElementsByClassName("hid_id");
if (id.length > 0) {
alert (id[0].value);
}
jsfiddle
try this:
var id = document.getElementsByClassName("hid_id")[0].value;

Categories

Resources