jquery - HTML, display label if drop down has just one value -
I am generating drop down dynamically by script.
If drop down is just an option value, it is possible to display it as a label.
This will display a drop down with 3 values.
& lt; Select & gt; & Lt; Options & gt; 1 & lt; / Options & gt; & Lt; Options & gt; 2 & lt; / Options & gt; & Lt; Options & gt; 3 & lt; / Options & gt; & Lt; / Select & gt; If it only shows one value, it is displayed as a label, is it possible with a combination of pure HTML or Jquery or both? I can use cleverness to check values and throw different HTML, but it will make my code for a long time because I have droplets of several drops.
& lt; Select & gt; & Lt; Options & gt; 1 & lt; / Options & gt; & Lt; / Select & gt; Update (Resoluted) Thanks to everyone StackWorflowers which helped
However, In order to copy, the code has expanded, if someone is looking for
, // a < Select & gt; To change, go to & lt; Label & gt; Tag if its just a value $ ('select'). Each (function () {if ($ (this) .find ('option'). Length === 1) {// Copy all the attributes from a given tag and save it in a variable. Var attributes = (This) .prop ("attributes"); var attributes_to = ''; $ .each (attributes_from, function) {attributes_to + = '' + this.name + '= "' + this.value + '"' ;}); // If you choose, copy its value from option. Attributes_to + = 'value = "' + $ (this) .find ('option'). Attr ('value') ('$' ('& lt; label' + attributes_ to + '/ & gt;' ') Html ($ (this) .text)}}}}}}};
< Pre = $ (select '). Each (function () {if ($ (This) .find (' option '). Length === 1) {$ (this) with .replace (function ( } {Return $ ('& lt; label / & gt;' html.html ($) .text ());});}}); Generate your dropdown After this, you can run this snippet to check each select elements. Demo:
Comments
Post a Comment