How do i get just 1 element JS [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
i am trying to add hcaptcha to my website but i am doing it in a strange way i want to get if the div with the class 'check' is hidden or not but i cannot do it.
here is my code
var x = document.getElementsByClassName("check")[0]
var y = window.getComputedStyle(x).display
alert(y)
it gives me the error 'cip.html:62 Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
at cip.html:62'

You need to verify that x is not undefined in case none of the elements have the class your looking for
var x = document.getElementsByClassName("check")[0]
if (x) {
var y = window.getComputedStyle(x).display
alert(y)
} else {
alert('not found')
}

Related

Javascript Couldn't Pickup InnerHTML [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
Here is my page element:
Then I tried to pick up the value demo#.... from the element class name user-name using these code:
var x = document.getElementsByClassName("user-name")[0].innerHTML;
console.log("x is: " + x);
It would return me:
x is:
Any idea how to fix this?
Looking at your approach, the code innerHTML should have worked, there might be some other issue which might be occurring.
Like 1) there might be other tag with same class name, 2) the username value might not be updated when you called the function.

Code always showing same output (Problem with If statements in JavaScript) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried making a tool that calculates battle out comes but this part of the code always displays: "DRAW!"
function battle()
{
var rawpower = document.getElementById('rawpower').value;
var rawpoweropp = document.getElementById('rawpoweropp').value;
if(rawpower > rawpoweropp){
alert("You won!");
} else if(rawpower < rawpoweropp){
alert("You lose!");
} else{
alert("Draw!");
}
}
The element with id="rawpower" is a paragraph tag <p>. These elements do not have values. So document.getElementById('rawpower').value returns undefined, and same for the other line. undefined is not less than undefined, nor is it greater than undefined, so you're going into the third case.

Javascript why do i get an error of 'addEventListener' undefined when everything works? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi i am trying to make an image slider for a client
I get this error in the console -> Uncaught TypeError: Cannot read property 'addEventListener' of undefined
Even tho everything works perfectly fine? what is going on with the console? i cant seem to figure it out
First of all your link has unappropriate contents, but, by the way, your code (main.js) is:
//IMAGE SCROLL
var slideLeft = document.getElementsByClassName('slideLeft');
var slideRight = document.getElementsByClassName('slideRight');
var imagesWrapper = document.getElementsByClassName('imagesWrapper');
var nrOfImageWrappers = imagesWrapper.length;
for(let i = 0; i <= nrOfImageWrappers; i++){
slideLeft[i].addEventListener('click', function(){
imagesWrapper[i].scrollLeft += -150;
});
slideRight[i].addEventListener('click', function(){
imagesWrapper[i].scrollLeft += 150;
});
}
let i = 0; i <= nrOfImageWrappers; i++, at the last turn, causes the index to be out-of-range because the length property counts the number of items but their index starts form 0, so, you need i < nrOfImageWrappers.

function that returns first object of a given array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
function first(array){
return first(0);
var first = ["gold","brown","green"21,1998];
console.log(first[0]);
trying to create a function that will return the first object of a given array, and am getting an error self[e.data.invoke.func].apply is not a function
not sure what I'm missing?
I tried this and it worked
function firstObject(array){
return array[0];
}
var first = ["Weresquirrel", "Werebear", "Werepanda", "Weremonkey"];
console.log(firstObject(first));

How to add var in get value in JavaScript? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How to add var in get value in JavaScript ?
I have 2 var
var page = $('#demoajax').val();
var name = $("#users").val();
This is mym old code
data:"server=Windows&actionfunction=showData&page="+page,
I want to add var name and value now=date to this line how can I do that ?
First I try this
data:"username="+name+"&server=Windows&actionfunction=showData&page="+page+"&now=date",
But not work.
You appear to have inserted your changes in the middle of the query string. Try this instead.
data:"server=Windows&username="+name+"&actionfunction=showData&page="+page+"&now=date",
Why do you have data:"username="+name+"&server=Windows&actionfunction=showData&page="+page+"&date=now",
switch date and now
Also lose the comma at end if not needed

Categories