Speak with system
Speak with system

Reputation: 43

Getting a object name

How to get a object name in javascipt

 lov_DgId_D_2.getElementsByTagName('a')[0].onclick= DgIdOnClick(???????????);

I got a spanid which I add onclick event now i want to get/extract this id 'lov_DgId_D_2' like name and put as argument to function DgIdOnClick.

Priview:

 lov_DgId_D_2.getElementsByTagName('a')[0].onclick= DgIdOnClick('lov_DgId_D_2');
 lov_DgId_D_3.getElementsByTagName('a')[0].onclick= DgIdOnClick('lov_DgId_D_3');
 lov_DgId_D_4.getElementsByTagName('a')[0].onclick= DgIdOnClick('lov_DgId_D_4');

U.t.c But not so simple to write it name in argument. THANKS! ;)

Upvotes: -1

Views: 50

Answers (1)

ˈvɔlə
ˈvɔlə

Reputation: 10242

Plain Javascript

I would recommend using an anonymous function as event handler.

lov_DgId_D_2.getElementsByTagName('a')[0].onclick = function (event) {
  DgIdOnClick(this.id);
} 

Note: In this case, the this keyword refers to the clicked HTML Element. But maybe you want to use event.target.id instead of this.id.

Javascript using jQuery framework

Since you have tagged your question with jQuery, I assume you implemented it in your project and would be pleased to hear a clean and simple solution using jQuery.

$('a', lov_DgId_D_2).click(function () {
  DgIdOnClick(this.id);
}

Upvotes: 3

Related Questions