mardi 6 août 2019

jquery attribute contains javascript

after hours of searching without result, I turn to you

the question is simple, with jquery can I put javascript in an attribute?

example

<div id="MyDiv" data-func="
    console.log('#MyDviv[data-func] called')
"></div>

the ultimate goal is: I have an object that has an action to do and I would like before I do this to call a function that checks can proceed.

exampe

<div id="MyDiv" data-action="next" data-func="
    ()=>{
        var valid = true

        /* ... */

        return valid===ture;
    }
"></div>

code in the library

$('#MyDiv').on('MyEvent',function() {

    /* ... */

    var buttonAct = $(this).attr('data-action');

    var validator = $(this).attr('data-func');
    var valid=false;

    /*
      code that executes the validator and puts the result in valid
    */

    if (valid)
    buttonNextPrev (buttonAct);

});

solutions like eval and things like that are not allowed, I need something that works like event example 'onclick'

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire