Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring form:select run JavaScript function on creating options

How can I execute a JavaScript function if select is creating rows/options?

<form:select path="field" multiple="false" size="8" cssClass="reg-selectField" onOptionsCreated="runThisJsFunction()">
    <form:options items="${field}" itemValue="id" itemLabel="name" cssClass="input-select" disabled="false"/>
</form:select>

Something like what I've added onOptionsCreated="runThisJsFunction()" on the code above.

like image 340
roybalderama Avatar asked Feb 18 '26 09:02

roybalderama


1 Answers

I don't think that we have a particular event which is triggered when options are loaded.You can do the same thing in document.ready jQuery function or in JavaScript something like

function options_loaded() {
    var e = document.getElementByNames('field')[0];
    if(e.options.length == 0){
        alert("No options loaded");
    } 
}
document.addEventListener("DOMContentLoaded", options_loaded, false);    

in jQuery

  $(function(){
       var optionsCount = $('[name="field"] option').length;
       if (optionsCount == 0){
          alert("No options are loaded"); 
       }
    });
like image 183
Ajinkya Avatar answered Feb 20 '26 21:02

Ajinkya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!