?
Solved

Change event not populating dropdown with data

Posted on 2014-11-19
3
Medium Priority
?
291 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
[X]
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
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Shahzad Fateh Ali earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

765 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