Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery function not executing inside PHP foreach loop
    text
    copied!<p>I'm trying to generate a number of checkboxes for a filter panel that when checked will refresh a list of products taking a specific parameter into account.</p> <p>In a foreach loop, I'm successfully creating the checkboxes by gathering the filter attributes from xml data called from an API</p> <pre><code>foreach ($attrib-&gt;attributeValues-&gt;attributeValue as $attribValue) { $attribValue = $attribValue-&gt;name; $attribValueID = $attribValue[@id]; $BODY .= sprintf("&lt;input type=\"checkbox\" name=\"%s\" value=\"%s\" id=\"%s\" checked%s=\"checked\" /&gt; %s ", $attribClass, $attribValue, $attribValue, $attribValue, $attribValue); </code></pre> <p>Next, I'm testing a basic jQuery function with two simple alerts to insure that the functions are actually being called</p> <pre><code>$BODY .= "&lt;script type=\"text/javascript\"&gt;"; $BODY .= "alert('Hello world!');"; $BODY .= "$(\"#".$attribValue."\").change(function() {"; $BODY .= "var $input = $(this);"; $BODY .= "alert(\"".$attribValue."\");"; $BODY .= "}).change();&lt;/script&gt;"; </code></pre> <p>When I run this on the page, the rendered html source code definitely shows that the jQuery function is being generated (multiple times, one for each checkbox created), but I'm not seeing either of the alerts - the one outside the function, or the one inside. It's my first time working with jQuery - any thoughts? Thanks as always!</p> <p>Edit: Sorry, here's the generated HTML source code for two checkboxes - this continues for all the checkboxes present, which are in the double digits.</p> <pre><code>&lt;input type="checkbox" name="Color" value="Black" id="Black" checkedBlack="checked" /&gt; Black &lt;script type="text/javascript"&gt;alert('Hello world!');$("#Black").change(function() {var = $(this);alert("Black");}).change();&lt;/script&gt; &lt;input type="checkbox" name="Color" value="Silver" id="Silver" checkedSilver="checked" /&gt; Silver &lt;script type="text/javascript"&gt;alert('Hello world!');$("#Silver").change(function() {var = $(this);alert("Silver");}).change();&lt;/script&gt; </code></pre>
 

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