Jump to content

[Javascript] How to get the clicked element?

Hello,how can i get the clicked button with Javascript?

I know how to do it with ID.
 

document.getElementById('myElement').addEventListener('click', function(){

   //do something

}

But how can i do this when i have a website with multiple "Save" buttons that have a class of "save" ?
How do i get the right button? So only that one thing will save? For forms I'm using submit.

I'm not using Jquery, but with Jquery i simply do $('.save') and i get all the elements with a class "save". Does Jquery just loop .addEventListener() on all the elements or how does it work?

I have this so far:
(not rly far but... meh)

	function cl(e){
		document.querySelectorAll(e).forEach(function(x){
			debug(x);
		});
	}

It outputs all the elements into the console, should i do the same but with .addEventListener?

AMD FX8320 | GTX660 | 4x4GB DDR3 | LG ZR2740W

Logitech Wireless Pro  | Logitech G413 | Nuforce uDAC5  | AKG K612

Link to comment
https://linustechtips.com/topic/1002637-javascript-how-to-get-the-clicked-element/
Share on other sites

Link to post
Share on other sites

I did this here:
 

function $(e,m,func){
		document.querySelectorAll(e).forEach(function(x){
			x.addEventListener(m,func);
		});
	}

Is yours faster?

AMD FX8320 | GTX660 | 4x4GB DDR3 | LG ZR2740W

Logitech Wireless Pro  | Logitech G413 | Nuforce uDAC5  | AKG K612

Link to post
Share on other sites

The function above adds a custom function to the addEventListener and i created another function:
 

function getValue(){
	console.log(this);
}

$('#nav_a a','click',getValue);

When i click on the link it outputs the anchor element, so far everything is okay.
My problem is when i try to get the attribute of "this" or "this.getValue", "this.value" everything results with undefined.

AMD FX8320 | GTX660 | 4x4GB DDR3 | LG ZR2740W

Logitech Wireless Pro  | Logitech G413 | Nuforce uDAC5  | AKG K612

Link to post
Share on other sites

26 minutes ago, mshaugh said:

What do you expect this.value to return?

Plan change, i found a easier way :D
But still have problems with it ughhh...
I created few pages in the "page" object. So the page object has multiple "test" pages.
 

content.loadPage(); //is a method that renders whe whole page from the page object.

content.loadPage(pages.article); //renders/opens the article. (works like a charm).

I want to load pages on hashchange, this isn't also a problem to get the hash:
 

loadPage(){
  let hash = location.hash.replace('#','');
  id('col_content').innerHTML = this.get(pages.article);
}

The issue is, when i get the hash, it's a string. So how to make something like:
"pages.hash". When i do this it's page.'myhash' as a string. How do i get the property ? WAIT!

I just remembered that there are two ways !
 

pages.article //Gets the article property from pages.
pages['article'] //Does the same and it uses a STRING! :D

Sometimes I'm just mad at myself, it took me 1-2h to realize that, i've tested each possible position of that function OMG, variables, even set a global variable to make it work! after 2h. Feels better than eating icecream and not getting fat.

AMD FX8320 | GTX660 | 4x4GB DDR3 | LG ZR2740W

Logitech Wireless Pro  | Logitech G413 | Nuforce uDAC5  | AKG K612

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×