Sup Sup Forums, do any of you know javascript?

sup Sup Forums, do any of you know javascript?
I'm trying to make an html button trigger a javascript function but it's not working and I don't get why.

Other urls found in this thread:

w3schools.com/js/js_whereto.asp
utf-8.jp/public/aaencode.html
utf-8.jp/public/jjencode.html
twitter.com/AnonBabble

***jquery is also acceptable

document.querySelector("#mybutton").addEventListener("click", function(){
// doSomething();
});

...

OP is a fag

function doShit(parameters)
{
//do shit here
}




poke me like you poke dick OP

The fucks up with the href="javascript:null;" thing?

also why are you using querySelector for something that has an ID? Use getElementById

>open the console
>fuck off

You can either have an onclick event that links to the function on the button


or add in an event listener


var btn = document.getElementById("button");
btn.addEventListener("click", function());

>typical JavaScript devs

If I were your boss and you show me a code like that, you'll be fired on the spot .

How would you do it then, boss?

Hint:
I won't touch the html. Everything will be done directly from the JS file or script tag (Yes, the function) . If you can't do this, you should stop writing JS

>or add in an event listener
>event listener
Once you've learnt to read, get your head out of your ass, it's case to case dependent. In some cases, an onclick function would serve better.

index.html


index.js
window.addEventListener('mousemove', checkCursor, false);

function checkCursor(event) {
const btn = document.querySelector('#button');
if (event.buttons === 1) {
if (event.clientX >= btn.offsetLeft && event.clientX = btn.offsetTop && event.clientY

He's just trying to make a suggestion on Sup Forums fuck off faggot

>make your website not work without javascript enabled

does the js file basically act like a class file in c++? where it's just the function def and you can call it from the linked html file?

I wouldn't call it a class file, you're just putting the JS in its own file and referencing/calling it from the html

what's the proper way to link them? I keep fucking it up somehow

Lol! You don't don't seem to get it , do you? Never put a function call in tags. It's bad practice and shows your ineptitude. What if you have 20 buttons? Will you add the same function call to all 20?

console.error('kill me'), 1000);
}

This might help. You should probably read a few more chapters too w3schools.com/js/js_whereto.asp

ng-click="angularIsAwesome()"

it's not much different to have 20 event listeners than 20 buttons with function calls

fantastic

needsmorejquery.com

You're all terrible javascript

Then why do we've setAttribute?
You're basically repeating yourself and wasting time/money, depending on your environment

jQuery is for losers and people with downs

I thought of a better way



var script = document.createElement('script');
script.innerHTML = `var btn = document.createElement('button');
btn.setAttribute('onclick', 'doStuff()');
document.body.appendChild(btn);

function doStuff() {
// shit
}`;
document.body.appendChild(script);

Beat that for asinine ways to do stuff

literally the same thing as onlick="";
but +1 for AngularJS.

>onclick=
for dumbs like you JS is misunderstand and hated.

>JS is misunderstand
Found you guy

>having 20 buttons on one page

>It's bad practice and shows your ineptitude.

It shows your inflexible and narrow thought process if you think there's only one correct method for everything.

I based my life on anime and JavaScript. Now I own more than a few thousand rupees and live in Pune (Silicon Valley 2.0 for you non poo in loos).

>2016
>Not using Kawaii.js

゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`*/ ['_']; o=(゚ー゚) =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); (゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ,゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ,゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; (゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\'; (゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];(゚Д゚) [゚o゚]='\"';(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+(゚Д゚)[゚o゚]+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ ((o^_^o) - (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+((゚ー゚) + (o^_^o))+ (o^_^o)+ (゚Д゚)[゚o゚]) (゚Θ゚)) ('_');

Is this an actual library? Please elaborate

... herp a derp ...

function someFunction() {

}

Everything else is more complex than it needs to be. People have lost their way.

Nah, its just JS encoding utf-8.jp/public/aaencode.html
There is also jjencode utf-8.jp/public/jjencode.html

Step 1 is to install React.
Step 2 is to create a React component.
Step 3 is to add your button.

jQuery is plebshit and Angular is on its way. React master race.

Vue master race.

Polymer is where its at yo

Well if u can have a js switch that does all the job that way u can bind multiple buttons to the same function or just to different ones having all on the same place... something like

Button1
Button2
Button3
.
.
.

function myFunc(el) {
console.log(el.id);
}

let buttons = document.getElementsByTagName('button');
for (let button of buttons) {
button.onclick = function() {
myFunc(this)
}
}

holy shit thats pretty funny, how does this work?

Another user seeking help.
I'm new and I've been trying to do a few things with js, now I want to click those images and show their alt on the"show" id. But I'm can't do it.

give those img's id to add the event listeners but something like this:
let show = document.getElementById('show');
let imgs = document.querySelectorAll('img');
for(let img of imgs) {
img.addEventListener('click', function() { show.innerText = img.alt });
}

I think something like that would work

Click This