Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you're looking to do is very easy: whether it's a good way to go is more questionable. Let's start with the meat of your question: how to create a function that incorporates a variable name (in your case, you want functions <code>setCheckboxes1</code>, <code>setCheckboxes2</code>, etc.).</p> <p>In JavaScript, functions are first-class citizens, so you don't even really need to do this, but let's assume for a moment that you do. When you declare a function in JavaScript, you aren't really declaring a function so much as you are a method on the global object. In a browser, that global object is simply called <code>window</code>. So, if you wanted to create all of your functions, you would simply need the following:</p> <pre><code>for(var i=0;i&lt;l;i++) { var value=jArray[i]; window['setCheckboxes'+value(act)] = function() { //... } } </code></pre> <p>As an aside, I don't think that <code>value(act)</code> is going to give you what you're looking for. You're invoking <code>value</code> as if it was a function, and JSON encoding an array is not going to return any functions. I suspect what you are looking for is <code>value.act</code> (assuming <code>act</code> is a property of each element in the array).</p> <p>Then you can just call each of the functions as normal.</p> <p>Allow me to give you a little advice, though: your JavaScript strikes me as very overcomplicated. JavaScript is a powerful and expressive language, and even though I haven't spent a lot of time grokking exactly what you're trying to do, everything about your code just screams "too much effort". I think what you're doing is going to be brittle, buggy, and hard to fix.</p> <p>Since you're using PHP, you probably think more procedurally, which is okay. But keep in mind that JavaScript is a function-based, dynamic language. You can pass around functions, arrays and objects as easy as breathing. Also, whenever you're using a back-end language (like PHP) combined with a front-end language (like JavaScript), you're always faced with the decision of how much work to do on the back end, and how much work to do on the front end. I would encourage you to do as much as possible on the front-end, with minimal processing in PHP. You'll find that JavaScript is more than up to the task.</p> <p>Also, I would encourage you to look into using jQuery if you can. Whenever you're doing a lot of manipulation in the browser, it will save you inordinate amounts of time and trouble.</p> <p>Good luck with your problem!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload