2
<div id='dgok' data-fn="delbanner('banners')">OK</div>

js

$('#dgok').click(function(){
    let fn = $(this).attr('data-fn');
    console.log(fn) // delbanner('banners')
    fn();
});

Error:

Uncaught TypeError: fn is not a function

How to execute delbanner('banners') in this scenario (clicking on dgok)?

3
  • What is 'banners' here? Please post the complete question with proper code in context. Commented Jul 23, 2018 at 6:42
  • @UtkarshPramodGupta, banners is an argument of the function. Commented Jul 23, 2018 at 6:43
  • You can use eval, but it's not a great idea. Why do you need to pass functions around in data- attributes? Commented Jul 23, 2018 at 6:44

6 Answers 6

1

Avoiding eval you may try an approach based on accessing functions via some container (window for example):

const fnContainer = window;
$('#dgok').click(function(){
    const fn = $(this).attr('data-fn');
    const fnName = fn.substring(0, fn.indexOf('(')); // 'delbanner'
    const fnArg = fn.substring(fn.indexOf('(') + 2, fn.length - 2); // 'banners'
    if (typeof fnContainer[fnName] === 'function') {
      fnContainer[fnName].apply(fnContainer, [fnArg]); // window[fnName](fnArg);
    }
});

Here I assume that your function is defined on window and has 1 string argument.


Having function argument in a separate data-attribute the approach may look a bit simplier

const fnContainer = window;
$('#dgok').click(function(){
    const fnName = $(this).attr('data-fn');
    const fnArg = $(this).attr('data-arg');
    if (typeof fnContainer[fnName] === 'function') {
      fnContainer[fnName].apply(fnContainer, [fnArg]); // window[fnName](fnArg);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

it seems ok. what if I have argument in separate data attribute - <div id='dgok' data-fn="delbanner" data-arg='banners'>OK</div>
1

this error is correct fn is not function .

use this

$('#dgok').click(function(){
    let fn = () => $(this).attr('data-fn');
    console.log(fn) // delbanner('banners')
    fn();
});

Comments

0

You could use eval but it's never a good idea. EVER

let fn = $(this).attr('data-fn');
eval(fn) // since fn is a string. it will evaluate whatever is in that string

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/eval

Comments

0

You need to use eval(fn) to treat that as a function call:

$('#dgok').click(function(){
    let fn = $(this).attr('data-fn');
    console.log(fn) // delbanner('banners')
    eval(fn);
});

function delbanner(param){
  console.log(param);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn="delbanner('banners')">OK</div>

Comments

0

Use eval.

Try this: :)

$('#dgok').click(function(){
    let fn = $(this).attr('data-fn');
    console.log(fn); 
    eval(fn);
});

Comments

0

You can use eval

function delbanner() {
  console.log('m')
}
$('#dgok').click(function() {
  console.log('a')
  let fn = $(this).attr('data-fn');
  eval(fn);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn="delbanner('banners')">OK</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.