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 4 years ago.
Improve this question
This simple function doesn’t execute when called. When taken out of the function it runs but not in the function. What gets the function to run?
HTML
<img id = “hide” src = “mypic.jpg”>
Javascript
Function show {
var x = document.getElementById(“hide”);
x.style.display = “none”
}
show();
You have the wrong declaration for a function
Function show {
var x = document.getElementById(“hide”);
x.style.display = “none”
}
show();
should be written as:
function show() {
var x = document.getElementById('hide');
x.style.display = 'none';
}
show();
<img id ="hide" src ="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" />
There is a couple of minor issues in your javascript, it should look like:
function show() {
var x = document.getElementById(“hide”);
x.style.display = “none”;
}
show();
In your code some basics are missing
"Function" must be "function"
function name must be with parenthesis i.e. functionName(){};
Your function declaration must be like this
function show(){}
Then you have to call the same function on some event like onClick() etc.
Here is the link you can go through JAVASCRIPT
Hope this helps you...Cheers!
Related
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 1 year ago.
Improve this question
I have a script (config.js) at the base of every page. But, that script contains code that isn't used on every page. The console returns unused or undeclared variables as undefined.
let currentURL = document.location.href;
function redirectURL() {
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
redirectURL();
This is what's returned.
How do I fix this?
You need to check whether the redirect element exists before trying to use it.
function redirectURL() {
if (typeof redirect == 'undefined') {
return;
}
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
This will allow the function to work without error on pages that don't define redirect.
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 have written this javascript code to add images each after a delay of 3 seconds but this code is not working kindly help.
function start() {
while(true) {
setTimeout(addObstracles(), 3000)
}
}
function addObstracle() {
var element = document.createElement('img');
element.id = 'obs';
element.className = 'obstracleAnimation';
element.src = 'enemy.png';
element.style.position = 'absolute';
element.style.top = '0px';
element.style.left = '100%';
element.style.width = '150px';
element.style.height = '100px';
document.body.appendChild(element);
}
setTimeout takes a function reference. You have a function invokation.
There are multiple ways to write this out:
setTimeout('addObstracles()', 3000) //i don't like this way
setTimeout(addObstracles, 3000) // pass the reference (I like this way!)
setTimeout(function() {
addObstracles()
}, 3000) //I like this when `addObstracles has parameters!
Like Sterling mentioned in his anser, you are calling the function addObstracles instead of passing it to setTimeout, just i want to add to it that because you are setting timout functions on fixed intervals you can use setInterval function instead:
function start() {
setInterval (addObstracles, 3000);
}
JSFiddle
There are multiple issues with this code. I will add to the answers the following:
be careful what function you define and what function you call: something != somethings
be careful where you call your start() function: it has to be after the body tag or body.appendChild won't exist
be careful how you style your element. With the current styling, it is not displayed at all. I have just removed element.style.position = 'absolute'; to make it visible
make sure the image you are trying to display is in the same folder as the script
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am working on a javascript quiz programme , and i have return a function to check what difficulty level the user wants . below is the code and the jsfiddle :
function getdifficulty(){
var j = 0;
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == easy){
j = questionseasy[0];
}
else if(level == intermediate){
j = questionseasyenuf[0];
}
else{
j = questionshard[0];
}
alert("you did it");
}
getdifficulty();
Jsfiddle here
now the problem is the the alert is not showing up ? whats the problem with this short piece of code ? (In the real programme though i will not use an alert but return statement , i even tried using document.write or console.log but none worked) .
prompt() returns a string. You need to make your comparisons strings by encapsulating them in double quotes (").
function getdifficulty(){
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == "easy"){
alert("easy");
}
else if(level == "intermediate"){
alert("intermediate");
}
else{
alert("hard");
}
}
getdifficulty();
JSFiddle
Also, in the implementation you have provided in your post, questionseasy, questionseasyenuf, and questionshard will not be defined. You must bring them into the scope of the function before you can start using them.
questionseasy is undefined. You can trace javascript as it runs in your browser console and see this.
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 8 years ago.
Improve this question
I'm following a tutorial online on how to make a to do list, found here:
https://www.youtube.com/watch?v=MURDw0-BiEE
I've following it pretty closely, but it won't work. Browser states the appendChild is null. The tutorial is 2 years old, could a part of the code be outdated? I had it sending alerts through the button, but when I changed to the appendChild things stopped working.
I'm pretty new to this and really appreciate the help.
function addNewItem() {
var listItem = document.createElement("li");
listItem.innerText = "Hello";
list.appendChild(listItem);
}
var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
addNewItem(document.getElementById("todoList"));
};
And here's the related part of the HTML:
<p><button id="btnAdd">New Item</button></p>
<ul id="todolist">
</ul>
<script src="todo.js"></script>
</body>
You have defined your function without arguments and then you try to pass one :
function addNewItem(list) {}
P.S. : You also tried to getElementById todoList instead of todolist, so it also gave you error :
addNewItem(document.getElementById("todolist"));
JSFiddle
You've missed function parameter, list:
function addNewItem(list) {
var listItem = document.createElement("li");
listItem.innerText = "Hello";
list.appendChild(listItem);
}
var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
addNewItem(document.getElementById("todoList"));
};
You are passing a parameter to the function addNewItem() with addNewItem(document.getElementById("todoList")), but you aren't declaring this parameter in your function. So the variable list becomes null and appendChild() does not work. So change your first line:
function addNewItem() {
to
function addNewItem(list) {
and it will work.
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 8 years ago.
Improve this question
There seems to be a lot of these errors on SO but I can't find any to relate to.
The error is: SyntaxError: missing : after property id and it's complaining about this line: var msg = ""; (Line 3).
function checkSubmission()({
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0){
msg+="LMS Name Required.<br/>";
}else{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex)) {
} else {
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lots more code here...
Any ideas? FYI I'm a complete newbie (for now...).
Thanks in advance!
The code in your comment on #Shoaib Raza, prompted me to post this. (please accept his answer rather than mine, since his was first.)
Quote OP Comment:
FYI, I changed it to just function checkSubmission( var msg = ""; ... and the error still appeared.
And that's totally wrong too. checkSubmission( ... is not what his answer is showing, so the new error message is accurate: SyntaxError: missing ( before { -> function checkSubmission{
This line of your original code contains the original syntax error...
function checkSubmission()({
should be this...
function checkSubmission() {
Notice the difference? You simply need to remove the extra ( and nothing else.
Change your function to :
function checkSubmission()
{
var msg = "";
$('#validation').html(msg);
if ($("#lms_name").val().length<=0)
{
msg+="LMS Name Required.<br/>";
}
else
{
var value = $('#lms_name').val();
var regex = new RegExp(/^[a-zA-Z0-9 ]{2,}$/);
if(value.match(regex))
{
}
else
{
msg+="<b>Error on LMS name : </b>Numbers and letters only. Minimum of two characters.<br/>";
}
}
//lot more code in the function here.. if any
}
Hope this helps.