Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: Only first of two .click() events runs when doing it using .click, .trigger()
    primarykey
    data
    text
    <p>I think I've seen a lot of similar questions to mine, but I have found no answer so far. I adapted some dependent drop downs found on the web, which work perfectly when clicked by the user: You click on the first list (select) and when you choose an item that has "children", these show on a second drop down and so on. The problem began when I wanted to pass default values. The first list got its value, nu problem. Then, to generate the second drop down I added a .click() event to the corresponding Id. It actually shows the second drop down and also the selected element (passed from the drop down populating function). HOWEVER, once loaded, no matter how many actual clicks or code mimmicking clicks, the second drop down does not run it's click function, it onle lets you select, but does not generate a third drop down, which, when not passing any values, works perfectly.</p> <p>What could be wrong?</p> <p>Here's the main JS code, which is located just before the body of the page:</p> <pre><code>var demo = {}; demo.globals = { princ: 0, princ2: 0, princ3: 0 } $(document).on('ready', function() { $(document).on('click', '#lprinc', function() { var value = $(this).val(); if (value==0){ $('#lprinc2').html('&lt;option value=""&gt;----&lt;/option&gt;').attr('disabled', true); $('#lprinc2').css({ 'visibility': 'hidden'}); } else{ $('#lprinc2').removeAttr('disabled'); $('#lprinc2').css({ 'visibility': 'visible'}); $('#lprinc3').html('&lt;option value=""&gt;----&lt;/option&gt;').attr('disabled', true); $('#lprinc3').css({ 'visibility': 'hidden'}); if (value != demo.globals.princ) { demo.checkDropdown('princ', 'princ2', value, val2); demo.globals.princ = value; } } }); if (val1!=''){ $('#lprinc').trigger('click'); } $(document).on('click','#lprinc2', function() { var value = $(this).val(); if (value==0){ $('#lprinc3').html('&lt;option value=""&gt;----&lt;/option&gt;').attr('disabled', true); $('#lprinc3').css({ 'visibility': 'hidden'}); } else{ $('#lprinc3').removeAttr('disabled'); $('#lprinc3').css({ 'visibility': 'visible'}); if (value != demo.globals.princ2) { demo.checkDropdown1('princ2', 'princ3', value, val3); demo.globals.princ2 = value; } } }); if (val2!=''){ $('#lprinc2').trigger('click'); } if (val3!=''){ $('#princ3').val(val3); } }); demo.checkDropdown = function(dropDownName, nextDropDown, value, valorpass) { jQuery.getJSON('/mod/update.php', { id : nextDropDown, value : value, val: valorpass}, function(data) { if (!data.error) { $('#lprinc2').html(data.list).removeAttr('disabled'); } else { $('#lprinc2').html('&lt;option value=""&gt;----&lt;/option&gt;').attr('disabled', true); $('#lprinc2').css({ 'visibility': 'hidden'}); $('#lprinc3').html('&lt;option value=""&gt;----&lt;/option&gt;').attr('disabled', true); $('#lprinc3').css({ 'visibility': 'hidden'}); } }); $("select[name='" + nextDropDown + "']").html(dropDownHtml); } demo.checkDropdown1 = function(dropDownName, nextDropDown, value, valorpass) { jQuery.getJSON('/mod/update.php', { id : nextDropDown, value : value, val: valorpass}, function(data) { if (!data.error) { $('#lprinc3').html(data.list).removeAttr('disabled'); } else { $('#lprinc3').html('&lt;option value=""&gt;----&lt;/option&gt;').attr('disabled', true); $('#lprinc3').css({ 'visibility': 'hidden'}); } }); $('#lprinc3').html(dropDownHtml); </code></pre> <p>Any help would be really appreciated. I have tried .one, .trigger(), .triggerHandler(), .click() to no avail. I have been going nuts overt his one... </p> <p>Added: update.php:</p> <pre><code>if (!empty($_GET['value'])) { $value = $_GET['value']; $valor= $_GET['val']; try { $objDb = new PDO('mysql:host=localhost;dbname=XXXXX', 'XXXXX', 'XXXXX'); $objDb-&gt;exec('SET CHARACTER SET utf8'); $sql = "SELECT * FROM `categories` WHERE `master` = ?"; $statement = $objDb-&gt;prepare($sql); $statement-&gt;execute(array($value)); $list = $statement-&gt;fetchAll(PDO::FETCH_ASSOC); if (!empty($list)) { $out = array('&lt;option value="0"&gt;Seleccione...&lt;/option&gt;'); foreach($list as $row) { $selstrg = "&lt;option value='".$row['id']."' "; if ($row['id']==$valor){ $selstrg = $selstrg." selected ='selected' "; } $selstrg = $selstrg."&gt;".$row['name']."&lt;/option&gt;"; $out[] = $selstrg; } echo json_encode(array('error' =&gt; false, 'list' =&gt; implode('', $out))); } else { echo json_encode(array('error' =&gt; true)); } } catch(PDOException $e) { echo json_encode(array('error' =&gt; true)); } } else { echo json_encode(array('error' =&gt; true)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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