Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,201 members, 7,818,669 topics. Date: Sunday, 05 May 2024 at 09:25 PM

Please Help A Script Kiddie Out With Javascript - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help A Script Kiddie Out With Javascript (1340 Views)

My Semester GPA Calculator With Javascript / Help A Beginner With Javascript / How Do I Create Responsive Slider With Javascript (2) (3) (4)

(1) (Reply) (Go Down)

Please Help A Script Kiddie Out With Javascript by donlyone(m): 10:53am On May 31, 2016
Please gurus in the house, I am stuck and I need your help with JavaScript.

I am trying to apply JavaScript to a page on PC, but whenever I call a function, only the first function works. The other ones don't work. I have to remove other functions before a particular function will work.

Even when I am using only one function, and I call the same function a second time to use it on another element, it doesn't work.

What am I doing wrong?

Parts of the code posted below:


var monthsY = ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];

function greeT(){
switch(new Date().getHours()){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
greet = "Good morning";
break;

case 12:
case 13:
case 14:
case 15:
case 16:
greet = "Good afternoon";
break;

case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
greet = "Good evening"
break;
}
return greet;
}


function salut(y){
document.getElementById(y).innerHTML=greeT();
}

salut("salut"wink;


var mo = function(x){
s=document.getElementById(x);
s.style.backgroundColor="green";
s.style.color="black";
s.style.fontSize="35px";
}

mo("mo"wink;
Re: Please Help A Script Kiddie Out With Javascript by Nobody: 11:05am On May 31, 2016
Try adding comments to your codes so another can know what you are trying to do.
Re: Please Help A Script Kiddie Out With Javascript by donlyone(m): 12:49pm On May 31, 2016
mbatuku2:
Try adding comments to your codes so another can know what you are trying to do.

Thanks for the reply.

Note that I have two functions I'm trying to use,
salut() and mo().

If I call the functions in this order:
salut()
mo()

salut() will work while mo() will not work.

If change the order like this:
mo()
salut()

Then mo() will work while salut() will not work.
All other code I add to the JavaScript does not work except the first one.

that's my problem.
Re: Please Help A Script Kiddie Out With Javascript by hollyfat(m): 1:37pm On May 31, 2016
donlyone:


Thanks for the reply.

Note that I have two functions I'm trying to use,
salut() and mo().

If I call the functions in this order:
salut()
mo()

salut() will work while mo() will not work.

If change the order like this:
mo()
salut()

Then mo() will work while salut() will not work.
All other code I add to the JavaScript does not work except the first one.

that's my problem.

Trying using your browser console to track the error.
Re: Please Help A Script Kiddie Out With Javascript by donlyone(m): 5:15pm On May 31, 2016
hollyfat:


Trying using your browser console to track the error.

After applying all the tips I got online, I still keep getting this error on browser console :

Uncaught TypeError: cannot set property 'innerHTML' of null
Re: Please Help A Script Kiddie Out With Javascript by hollyfat(m): 5:35pm On May 31, 2016
donlyone:


After applying all the tips I got online, I still keep getting this error on browser console :

Uncaught TypeError: cannot set property 'innerHTML' of null
This might be due to the fact that you are trying to change the content of an element that does not exist
Re: Please Help A Script Kiddie Out With Javascript by forap(m): 8:00pm On Jun 02, 2016
It will be clearer if you pasted the html code also.

BTW, you should replace the switch statement in the salute function with something like this:


var hour = new Date().getHours();
if (hour <= 11) {
greet = "Good morning";
} else if (hour <= 16) {
greet = "Good afternoon";
} else {
greet = "Good evening";
}
Re: Please Help A Script Kiddie Out With Javascript by Xtracoool(m): 8:52pm On Jun 02, 2016
'x' and 'y' are suppose to be IDs of HTML elements on your page. Check that the values you pass to each of the 2 functions match IDs in your HTML.
Re: Please Help A Script Kiddie Out With Javascript by oluwaskulboy(m): 12:32pm On Jun 03, 2016
DEVELOPING AN ANDROID APP, WHAT YOU SHOULD KNOW. http://techzone.com.ng/2016/05/05/developing- android-app-know/
Re: Please Help A Script Kiddie Out With Javascript by Nobody: 2:38pm On Jun 03, 2016
Hi,
The problem in your code is in the way you have created the functions.

In JavaScript, there are two methods to create functions.
In the first method, the function is saved in a variable

For example,

var methodName = function(){
}
Using this method, you have to define the function before calling it.

This will work
var greet = function(){
//print greeting
}
greet()//method call after method definition
This won't work
greet() //method call before method definition
var greet= function(){
//print greeting
}

Method 2 is easier. You define the function like this

function greet(){
}

This way, no matter where you place your method call, the function will run.


It's safer to define your functions using the 2nd method. Make the change to the mo function in your code.
I hope this explanation is clear enough.
Re: Please Help A Script Kiddie Out With Javascript by donlyone(m): 9:44pm On Jun 03, 2016
Thank you guys.

I really appreciate this.

1 Like 1 Share

(1) (Reply)

Is Learning Visual Basic Under Asp.net In 2017, A Waste / My Personal Page / URL For Validation During Remita Integration; How To Set It Up

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.