function with name as a data value of a div










0














I want to execute a function which name is a data attribute value of a div:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact');
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn='delimg'>OK</div>





TypeError: Illegal invocation...at delimg (images.js:75)



Any help?










share|improve this question



















  • 2




    What's happen if we change afn, src to afn: afn, src: src?
    – Foo
    Nov 12 at 11:42











  • @TânNguyễn, the same - Illegal invocation...
    – puerto
    Nov 12 at 11:45










  • I believe that the problem is not with how the function delimg is being invoked, rather it seems to be related to the kind of data which is being posted. It seems as though $('.imgact') is not a valid piece of data to post, presumably because it's a dom element
    – OliverRadini
    Nov 12 at 11:46






  • 1




    Then, you can try again with afn: 1, src: 1. If it works, the problem comes from the way you get afn and src. If it is, try again with single property afn: afn and src: src. Then, you can know which makes the problem.
    – Foo
    Nov 12 at 11:50















0














I want to execute a function which name is a data attribute value of a div:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact');
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn='delimg'>OK</div>





TypeError: Illegal invocation...at delimg (images.js:75)



Any help?










share|improve this question



















  • 2




    What's happen if we change afn, src to afn: afn, src: src?
    – Foo
    Nov 12 at 11:42











  • @TânNguyễn, the same - Illegal invocation...
    – puerto
    Nov 12 at 11:45










  • I believe that the problem is not with how the function delimg is being invoked, rather it seems to be related to the kind of data which is being posted. It seems as though $('.imgact') is not a valid piece of data to post, presumably because it's a dom element
    – OliverRadini
    Nov 12 at 11:46






  • 1




    Then, you can try again with afn: 1, src: 1. If it works, the problem comes from the way you get afn and src. If it is, try again with single property afn: afn and src: src. Then, you can know which makes the problem.
    – Foo
    Nov 12 at 11:50













0












0








0







I want to execute a function which name is a data attribute value of a div:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact');
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn='delimg'>OK</div>





TypeError: Illegal invocation...at delimg (images.js:75)



Any help?










share|improve this question















I want to execute a function which name is a data attribute value of a div:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact');
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn='delimg'>OK</div>





TypeError: Illegal invocation...at delimg (images.js:75)



Any help?






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact');
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn='delimg'>OK</div>





$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact');
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dgok' data-fn='delimg'>OK</div>






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 11:44









OliverRadini

2,653627




2,653627










asked Nov 12 at 11:39









puerto

918411




918411







  • 2




    What's happen if we change afn, src to afn: afn, src: src?
    – Foo
    Nov 12 at 11:42











  • @TânNguyễn, the same - Illegal invocation...
    – puerto
    Nov 12 at 11:45










  • I believe that the problem is not with how the function delimg is being invoked, rather it seems to be related to the kind of data which is being posted. It seems as though $('.imgact') is not a valid piece of data to post, presumably because it's a dom element
    – OliverRadini
    Nov 12 at 11:46






  • 1




    Then, you can try again with afn: 1, src: 1. If it works, the problem comes from the way you get afn and src. If it is, try again with single property afn: afn and src: src. Then, you can know which makes the problem.
    – Foo
    Nov 12 at 11:50












  • 2




    What's happen if we change afn, src to afn: afn, src: src?
    – Foo
    Nov 12 at 11:42











  • @TânNguyễn, the same - Illegal invocation...
    – puerto
    Nov 12 at 11:45










  • I believe that the problem is not with how the function delimg is being invoked, rather it seems to be related to the kind of data which is being posted. It seems as though $('.imgact') is not a valid piece of data to post, presumably because it's a dom element
    – OliverRadini
    Nov 12 at 11:46






  • 1




    Then, you can try again with afn: 1, src: 1. If it works, the problem comes from the way you get afn and src. If it is, try again with single property afn: afn and src: src. Then, you can know which makes the problem.
    – Foo
    Nov 12 at 11:50







2




2




What's happen if we change afn, src to afn: afn, src: src?
– Foo
Nov 12 at 11:42





What's happen if we change afn, src to afn: afn, src: src?
– Foo
Nov 12 at 11:42













@TânNguyễn, the same - Illegal invocation...
– puerto
Nov 12 at 11:45




@TânNguyễn, the same - Illegal invocation...
– puerto
Nov 12 at 11:45












I believe that the problem is not with how the function delimg is being invoked, rather it seems to be related to the kind of data which is being posted. It seems as though $('.imgact') is not a valid piece of data to post, presumably because it's a dom element
– OliverRadini
Nov 12 at 11:46




I believe that the problem is not with how the function delimg is being invoked, rather it seems to be related to the kind of data which is being posted. It seems as though $('.imgact') is not a valid piece of data to post, presumably because it's a dom element
– OliverRadini
Nov 12 at 11:46




1




1




Then, you can try again with afn: 1, src: 1. If it works, the problem comes from the way you get afn and src. If it is, try again with single property afn: afn and src: src. Then, you can know which makes the problem.
– Foo
Nov 12 at 11:50




Then, you can try again with afn: 1, src: 1. If it works, the problem comes from the way you get afn and src. If it is, try again with single property afn: afn and src: src. Then, you can know which makes the problem.
– Foo
Nov 12 at 11:50












1 Answer
1






active

oldest

votes


















3














Without knowing exactly what $('.imgact') is, it's hard to know what it should be. I'm going to guess it's an input, though, so correct me if I'm wrong.



The issue is that you're trying to send data that jquery really doesn't want to let you send. $('.imgact') is an element, and if it's an input, we probably want its value rather than the element itself.



That would look like this:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>








share|improve this answer
















  • 1




    you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
    – puerto
    Nov 12 at 11:51











  • That's great, glad to hear it's working
    – OliverRadini
    Nov 12 at 11:52










  • afn, src is still invalid syntax. afn: afn, src: src should do it.
    – maryisdead
    Nov 12 at 11:52






  • 1




    @maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – OliverRadini
    Nov 12 at 11:53







  • 1




    @maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
    – OliverRadini
    Nov 12 at 12:41










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261385%2ffunction-with-name-as-a-data-value-of-a-div%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Without knowing exactly what $('.imgact') is, it's hard to know what it should be. I'm going to guess it's an input, though, so correct me if I'm wrong.



The issue is that you're trying to send data that jquery really doesn't want to let you send. $('.imgact') is an element, and if it's an input, we probably want its value rather than the element itself.



That would look like this:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>








share|improve this answer
















  • 1




    you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
    – puerto
    Nov 12 at 11:51











  • That's great, glad to hear it's working
    – OliverRadini
    Nov 12 at 11:52










  • afn, src is still invalid syntax. afn: afn, src: src should do it.
    – maryisdead
    Nov 12 at 11:52






  • 1




    @maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – OliverRadini
    Nov 12 at 11:53







  • 1




    @maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
    – OliverRadini
    Nov 12 at 12:41















3














Without knowing exactly what $('.imgact') is, it's hard to know what it should be. I'm going to guess it's an input, though, so correct me if I'm wrong.



The issue is that you're trying to send data that jquery really doesn't want to let you send. $('.imgact') is an element, and if it's an input, we probably want its value rather than the element itself.



That would look like this:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>








share|improve this answer
















  • 1




    you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
    – puerto
    Nov 12 at 11:51











  • That's great, glad to hear it's working
    – OliverRadini
    Nov 12 at 11:52










  • afn, src is still invalid syntax. afn: afn, src: src should do it.
    – maryisdead
    Nov 12 at 11:52






  • 1




    @maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – OliverRadini
    Nov 12 at 11:53







  • 1




    @maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
    – OliverRadini
    Nov 12 at 12:41













3












3








3






Without knowing exactly what $('.imgact') is, it's hard to know what it should be. I'm going to guess it's an input, though, so correct me if I'm wrong.



The issue is that you're trying to send data that jquery really doesn't want to let you send. $('.imgact') is an element, and if it's an input, we probably want its value rather than the element itself.



That would look like this:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>








share|improve this answer












Without knowing exactly what $('.imgact') is, it's hard to know what it should be. I'm going to guess it's an input, though, so correct me if I'm wrong.



The issue is that you're trying to send data that jquery really doesn't want to let you send. $('.imgact') is an element, and if it's an input, we probably want its value rather than the element itself.



That would look like this:






$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>








$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>





$('#dgok').click(function() 
var fn = $(this).attr('data-fn');
window[fn]();
);

function delimg()
console.log('james'); // this works
let src = $('.imgact').val();
let afn = 'store';
$.post('images-pro.php', afn, src, function(data) // line 75
console.dir(data);
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class".imgact" />
<div id='dgok' data-fn='delimg'>OK</div>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 11:50









OliverRadini

2,653627




2,653627







  • 1




    you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
    – puerto
    Nov 12 at 11:51











  • That's great, glad to hear it's working
    – OliverRadini
    Nov 12 at 11:52










  • afn, src is still invalid syntax. afn: afn, src: src should do it.
    – maryisdead
    Nov 12 at 11:52






  • 1




    @maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – OliverRadini
    Nov 12 at 11:53







  • 1




    @maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
    – OliverRadini
    Nov 12 at 12:41












  • 1




    you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
    – puerto
    Nov 12 at 11:51











  • That's great, glad to hear it's working
    – OliverRadini
    Nov 12 at 11:52










  • afn, src is still invalid syntax. afn: afn, src: src should do it.
    – maryisdead
    Nov 12 at 11:52






  • 1




    @maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – OliverRadini
    Nov 12 at 11:53







  • 1




    @maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
    – OliverRadini
    Nov 12 at 12:41







1




1




you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
– puerto
Nov 12 at 11:51





you're right, thanks. The problem was with $('.imgact'). What I need is $('.imgact').attr('src')`.
– puerto
Nov 12 at 11:51













That's great, glad to hear it's working
– OliverRadini
Nov 12 at 11:52




That's great, glad to hear it's working
– OliverRadini
Nov 12 at 11:52












afn, src is still invalid syntax. afn: afn, src: src should do it.
– maryisdead
Nov 12 at 11:52




afn, src is still invalid syntax. afn: afn, src: src should do it.
– maryisdead
Nov 12 at 11:52




1




1




@maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– OliverRadini
Nov 12 at 11:53





@maryisdead what do you mean by invalid syntax? It's valid in es6 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– OliverRadini
Nov 12 at 11:53





1




1




@maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
– OliverRadini
Nov 12 at 12:41




@maryisdead it's really nice to use, just need to be careful with browser support, as it won't work in ie
– OliverRadini
Nov 12 at 12:41

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261385%2ffunction-with-name-as-a-data-value-of-a-div%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党