javascript - JQM 1.4.5 : disabling button breaks page rendering -


going through steep learning curve, experimenting various ux 'toys' require implement app. 1 of these disable button , enable on fly. following instructions of the book , wrote little snippet of code test out. clicking on "soap" runs series of chained promises, , toggles "soap1" button disabled prop.

my html/js

<div data-role="content">     <a href="#" id="btn_soap1" class="ui-input-btn ui-btn ui-mini ui-btn-inline ui-icon-back "        onclick="getinitialnotifications();">soap1</a>     <button id="btn_soap" class="ui-btn ui-btn-inline ui-mini ui-icon-bullets "             onclick="getinitialnotifications();">         soap     </button>      <script>          $("#btn_soap1").button({            // required initialization             disabled:false         });          $("#btn_soap").on("click", function () {              // bubbled onclick thingie in markup              var isdis = $("#btn_soap1").button("option","disabled");             $("#btn_soap1").button("option","disabled",!isdis);  //                var = $("#btn_soap1"); //                var classname = "ui-state-disabled"; //                if(but.hasclass(classname)) { //                    but.removeclass(classname); //                } else { //                    but.addclass(classname); //                }           });     </script> </div> 

intended rendering

enter image description here

broken rendering (all browsers , device sims , devices)

enter image description here

question : can see noob error in js cause side-effect. added (in comments) work-around, works specified, seems counter-intuitive.

edit: (from mr. duc nguyen's answer). breaks rendering adding initialization. if not there, exception whining calling function prior initialization when changing disabled state.

edit again : discovered jsfiddle, ... a fiddle reproduces this

edited: new answer basing on jsfiddle

you have gotten in interesting situation, points below:

  • jqm has auto-initaliasation merchanism, if want leverage that, have follow rules, or totally disable , initialisation yourself. jqm global config
  • you have 2 "buttons", 1 <a> , 1 <button>, disabling <a> never easy one, have here disabling html link
  • jqm might confuse <a> tag button widget, not! has same styling button, not button widget. button widget applying <button> , appropriate <input> type (it mentioned in documents in 1.2.0's days, couldn't find in 1.4.5 docs)

so, here how leverage jqm auto-initialisation:

    <a href="#" id="btn_soap1" class="ui-disabled" data-role="button" data-inline="true" data-icon="back">soap1</a>     <button id="btn_soap" data-inline="true">soap</button> 

notice on <a>:

  • the attribute data-role="button" tell jqm mark button
  • this classclass="ui-disabled" disable initially.

and how disable link <a> on-the-fly. notice adding class, won't work on specific infamous browsers, referring above stackoverflow answer more information.

    var isdis = $("#btn_soap1").hasclass("ui-disabled");     if (!isdis) {         $("#btn_soap1").addclass("ui-disabled");     } else {         $("#btn_soap1").removeclass("ui-disabled");     } 

again, can call .button([method]) on real button!

have on updated jsfiddle, have cleaned things bit.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -