chosen jquery not working for values fetched from database

I have been trying to add the chosen jquery for my selectbox which is fetching the values from database based on another. The normal selectbox works fine and all the options are getting displayed but the chosen jquery is not working. I won't put the script tags over here because the chosen jquery is working fine for hard coded values of host names.I have applied jquery for 'host_name' scrolling list.

This is what i tried

$( function() {
$(".host_name").chosen();  });

or:

   $( function() {
    $("#host_name").chosen().change(host_name);  })

or:

$( function() {
    $('#host_name').trigger('chosen:updated');
})

perl cgi code containing ajax for fetching data from database scrolling list:

my $JAVASCRIPT = <<'ENDJS';
function call(host)
{
//alert("in call");
var selos=document.getElementById("ostype");
var x=selos.options[selos.selectedIndex].text;

if (x){
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange=function() {
    if (this.readyState == 4 && this.status == 200) {
                                var obj=JSON.parse(this.responseText);
 var select = document.getElementById("host_name");
        for (var key in obj){

                                        var option = document.createElement('option');
                                        option.text = option.value = obj[key];
                                        select.add(option, 0);

                                        //document.getElementById('host_name').appendChild(var);
                                }
    }
  };
  xhttp.open("GET", "logic.cgi?ostype="+x, true);

  xhttp.send();

}
}

cgi code for html

 $q->start_td({-colspan=>2}),
       $q->scrolling_list(-name=>'ostype',
-size=>1,
-id=>'ostype',
-values=>['RHEL','Windows','SLES'],
-onClick=>'together()',
-onChange=>'call()'
),
$q->end_td,



"Host name",$q->font({-color=>'red'},"*") ,
$q->end_td,
 $q->start_td({-colspan=>2}),

$q->scrolling_list({-style=>'width:150px',
-name=>'host_name',
-class=>'host_name',
-id=>'host_name',
-size=>'3',
-values=>[],
-multiple=>'true'}
),
htmlajaxperlcgijquery-chosen

Answers

answered 6 days ago Yvonne Aburrow #1

$( function() {
$(".host_name").chosen();  });

The $(".host_name") jQuery selector selects all elements with the class hostname (e.g. <div class="host_name"></div>).

   $( function() {
    $("#host_name").chosen().change(host_name);  })

The $("#host_name") jQuery selector selects all elements with the id hostname (e.g. <div id="host_name"></div>)

$( function() {
    $('#host_name').trigger('chosen:updated');
})

You might want to check the chosen() documentation for usage examples, too.

There's an error in your commented-out bit of JavaScript:

//document.getElementById('host_name').appendChild(var);

It should be

//document.getElementById('host_name').appendChild(option);

comments powered by Disqus