jquery get not allowing x[i] inside of function [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to write a JQuery get that returns true if there is an embedded youtube video at the other end of the URL, and if so update the div to mention that.
My Code so far:
var x = document.getElementsByClassName("videoplace");
var lim=x.length;
for (var i = 0; i <lim; i++) {
$.get(x[i].innerHTML, function(data){if(data.indexOf("youtube.com/embed")>-1)
{
x[i].innerHTML = "Video Exists";
}
});
}
When I run this I get an error in the console that says "TypeError: Cannot set property 'innerHTML' of undefined" I have tried a plethora of alternatives and while the get function is working, and the if only fires if it returns true correctly, it doesnt correctly recognize where to put the "Video exists" text.

By the moment callback function(data) run for loop is over Correct call is
var x = document.getElementsByClassName("videoplace");
var lim=x.length;
for (var i = 0; i <lim; i++) {
(function(j){$.get(x[j].innerHTML, function(data){if(data.indexOf("youtube.com/embed")>-1)
{
x[j].innerHTML = "Video Exists";
}
}))(i);
}

x !== $x
var x = document.getElementsByClassName("videoplace");
var lim=x.length;
for (var i = 0; i <lim; i++) {
$.get(x[i].innerHTML, function(data){if(data.indexOf("youtube.com/embed")>-1)
{
x[i].innerHTML = "Video Exists";
}
});
}

Related

Why my if & else condition doesn't work correctly? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I use this code to add & remove the style , Part ( else ) only works and sets (display) to block, But it will not return the class if clicked again.
what's wrong?
var navDropDown = document.querySelectorAll('.menu-item-has-children > a');
for (let i = 0; i < navDropDown.length; i++) {
navDropDown[i].addEventListener('click', (e) => {
if (navDropDown[i].nextElementSibling.style.display = 'none') {
this.nextElementSibling.style.display = 'block';
} else {
this.nextElementSibling.style.display = 'none';
}
})
}
The line:
if (navDropDown[i].nextElementSibling.style.display = 'none') {
uses a single =, instead of ==. In Javascript, = is an assignment, which returns the value assigned. 'none' being not empty, it is converted into true, and thus the else will never be executed.

Javascript function causing error not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a simple function that seems to be causing an 'Uncaught ReferenceError: arraySort is not defined' whenever the function is called, in this case by a button and i cant see why any help would be brilliant.
Javascript
<script language="javascript">
var unsorted = ["Printer","Tablet","Router"];
var alphaOrder = [" ","A","a","B","b","C","c","D","d","E","e","F","f","G","g", //15
"H","h","I","i","J","j","K","k","L","l","M","m","N","n","O", //30
"o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v", //45
"W","w","X","x","Y","y","Z","z","0","1","2","3","4","5","6", //60
"7","8","9","'","?","!",".","\"","<",">","#",",","#","~","=", //75
"+","-","_","/","\\"];
function arraySort(array){
var sortedArray = [];
var letterNum = 0;
var numArray = [];
function letterToNum(){
for (var elementNum = 0; elementNum < array.length; elementNum++;){
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++;){
numArray[elementNum] = alphaOrder.indexOf(array[elementNum][letterNum]);
document.getElementById('tester1').innerHTML = numArray;
}
}
}
}
</script>
HTML
<button type = "button" onclick = "arraySort(unsorted)">Sort</button>
Remove the semicolon from the end of your loops.
for (var elementNum = 0; elementNum < array.length; elementNum++) {
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++) {
}
Few suggestions here
Wrap your logic inside script by window.onload
Don't mix your markup and javascript.
Try binding events at javascript end
Follow the above suggestions, your error will be fixed.

JavaScript json for-loop Syntax Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to eliminate a chunk of variables:
var security = [
{ name: 'X-Powered-By',
option: '' }
, { name: 'x-frame-options',
option: file.get('headers.xFrameOptions') }
, { name: 'X-XSS-Protection',
option: file.get('headers.xXSSProtection') }
, { // AND SO ON...}
]
That is looped with:
// Add Content Security Rules to the header
for(var i = 0; i < security.length; i++) {
res.setHeader(security[i].name, security[i].option);
}
In order to eliminate all those variables, I am trying to edit the for-loop in the following way:
for(var i = 0; i < file.get('headers.length; i++') {
res.setHeader(headers[i].name);
}
I am getting a syntax error and I am not sure if I am doing it correctly. An example would be very appreciated.
you have problems with your closing brackets and quotes, should be:
for(var i = 0; i < file.get('headers.length') ; i++) {
res.setHeader(headers[i].name);
}

Why does `document.getElementById(“#datepicker1”)` not find my element? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to show / hide a text input based on the selection of a drop down list.
Here’s a JSFiddle to what I am trying to do.
When I get into the function, I get a “TypeError: Cannot set property 'type' of null”.
This is the code:
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "text";
}
else {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "hidden";
}
};
You do not need the # when using getElementById. getElementById returns null if does not find the element. null does not have a property named type - therefore you get the error.
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("datepicker1"); //removed #
//console.log(y.type);
y.type = "text";
} else {
var y = document.getElementById("datepicker1"); //removed #
//console.log(y.type);
y.type = "hidden";
}
};
y is null, because the id of the element is probably datepicker1 and not #datepicker1
var y = document.getElementById("#datepicker1");
should be
var y = document.getElementById("datepicker1");

JS: Why FOR LOOP loops once? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why I get only one alert after run this code:
var poly = function()
{
this.disp = function()
{
for(var i=0; i<6; i++); //And I already found my problem. It is this ';'
{
alert('The number is '+i); //I get one alert: 'The number is 6'
}
}
}
test = new poly();
test.disp();
Thanks for any help!
This code works. Probably, in your real code, it looks like this:
var poly = function()
{
this.disp = function()
{
for(var i=0; i<6; i++)
{
}
alert('The number is '+i); //I get one alert: 'The number is 6'
}
}
That will cause i to loop from 0 to 6, after which it is alerted once.

Categories