Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Change event not populating dropdown with data

Posted on 2014-11-19
3
284 Views
Last Modified: 2014-11-20
I have been trying for the past few days to figure out how chosen.js handles change events with no success hence the post. I have tried so many options and configurations, that I am admitting defeat and hoping someone can help.

I have a very simple select which populates from mysql. This works ok. Now, when I trigger the change event, I can see that the data is being returned in firebug, but no data is present in the dropdown. So, I guess my question is, how the hell do you work with change events in chosen.js.

If I take chosen out of the equation, it works well. Where I am getting confused is why I can see the data in firebug but the select is not updating. many thanks.

FYI: Assume all libraries are loaded and working.

html

       
<div class="fieldset">
          <h1><span>Select a Customer</span></h1>
          <p>
            <select data-placeholder="Choose a customer..." class="chosen-select" style="width:250px;" name="dstr_customer" id="dstr_customer">
              <option value=""></option>
                <option value="DEMO">DEMO</option>
                <option value="DEMO2">DEMO2</option>
                <option value="DEMO3">DEMO3</option>
                <option value="DEMO4">DEMO4</option>
                <option value="DEMO5">DEMO5</option>
            </select>
            <span></span>
            <a href="javascript:void(0)" style="margin-left:10px;" class="tooltip" title="Please select a dept where the box to be destroyed is stored.">Help</a>
          </p>

        </div>

        <div class="fieldset">
          <h1><span>Select a Department</span></h1>
          <p>
            <select data-placeholder="Choose a dept..." class="chosen-select" style="width:250px;" name="dstr_dept" id="dstr_dept">
              <option value=""></option>

            </select>
            <span></span>
            <a href="javascript:void(0)" style="margin-left:10px;" class="tooltip" title="Please select a dept where the box to be destroyed is stored.">Help</a>
          </p>
          <div class="noDept"></div>
        </div>

Open in new window


js

$(function() {
    $("#dstr_customer").change(function() {
      $(this).after('<div id="loader"><imgages src="img/loading.gif" alt="loading files" /></div>');
      $.get('droptestback.php?cltdstrdept=' + $(this).val(), function(data) {
        $('#dstr_dept').html(data);
        $('#loader').slideUp(200, function() {
          $(this).remove();
          $("dstr_dept").trigger("chosen:updated");
          //$("#dstr_dept").prop('disabled', false);
        });        
      });
    });
  });

  $(function() {
    $("#dstr_customer").chosen({
      width: "260px"
    });
  });

  $(function() {
    $("#dstr_dept").chosen({
      width: "260px",
      placeholder_text_single: "Select Some Options"
    });
  });

Open in new window


droptestback.php

if (mysql_num_rows($result) > 0) {

    echo "<script type=text/javascript>\n";
    echo "$(function() {\n";
    echo "$(\".noDept\").html('')\n";
    echo "var newOption = \"<option value=''>Some Text</option>\"\n";

    echo "$(\"#dstr_dept\").attr(\"placeholder\", newOption)\n";
    echo "$(\"#dstr_dept\").prop('disabled', false)\n";
    echo "});\n";
    echo "</script>\n";

    //echo "<option value=\"\">Choose Your dept</option>";

    while($row = mysql_fetch_array($result)) {
    echo "<option value='$row[name]'>$row[name]</option>";
    }  



    } else {

    echo "<script type=text/javascript>\n";
    echo "$(function() {\n";
    echo "$('.noDept').html('ERROR: There are no departments that match that company. Please select another.').css({\"color\":\"red\", \"margin\": \"-6px 0 10px 22px\", \"font-size\": \"12px\", \"font-family\": \"Verdana, Geneva, sans-serif\"})\n";
    echo "$('#box_dstr').attr('data-placeholder', \"No boxes to display...\").prop('disabled', true)\n";
    echo "});\n";
    echo "</script>\n";

    //echo "<option value='No boxes in that dept'>No boxes in that dept</option>";

    }

Open in new window

0
Comment
Question by:peter-cooper
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Shahzad Fateh Ali earned 500 total points
ID: 40454327
As per the documentation, your code at line 2
$("#dstr_customer").change(function() {

Open in new window

Should be changed to
$("#dstr_customer").chosen().change(function() {

Open in new window


Also, do not user multiple $(function(){}) calls. Here is the final version of your code.

$(function() {

    $("#dstr_customer").chosen({
      width: "260px"
    }).change(function() {
	
      $(this).after('<div id="loader"><imgages src="img/loading.gif" alt="loading files" /></div>');
      $.get('droptestback.php?cltdstrdept=' + $(this).val(), function(data) {
        $('#dstr_dept').html(data);
        $('#loader').slideUp(200, function() {
          $(this).remove();
          $("dstr_dept").trigger("chosen:updated");
          //$("#dstr_dept").prop('disabled', false);
        });        
      });
    });
	
   $("#dstr_dept").chosen({
      width: "260px",
      placeholder_text_single: "Select Some Options"
    });
  });

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 40454503
Thank you very much Shahzad.
0
 

Author Closing Comment

by:peter-cooper
ID: 40454506
Thanks once again
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question