Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Possible Duplicate:
Call a JavaScript function name using a string? Call a JavaScript function name using a string?
javascript string to variable javascript string to variable

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

Possible Duplicate:
Call a JavaScript function name using a string?
javascript string to variable

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

Possible Duplicate:
Call a JavaScript function name using a string?
javascript string to variable

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

insert duplicate link
Source Link

Possible Duplicate:
Call a JavaScript function name using a string?
javascript string to variable

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

Possible Duplicate:
Call a JavaScript function name using a string?
javascript string to variable

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

Source Link
benhowdle89
  • 37.6k
  • 74
  • 208
  • 342

Javascript convert string into function name

I have this code:

var Functionify = function() {
 return {
 init: function(el, t) {
 var els = document.getElementsByClassName(el);
 var elsL = els.length;
 
 while(elsL--){
 //els[elsL].onclick = els[elsL].getAttribute(t);
 els[elsL].addEventListener('click', els[elsL].getAttribute(t), false);
 }
 }
 };
}();

Where el = 'myClassName' and t = 'data-id'

Now, 't' is a string, how can tell the addEventListener function to use 't' (a string) as a function name?

lang-js

AltStyle によって変換されたページ (->オリジナル) /