I have an input field which I want to use on mobile and desktop made with Jquery Mobile.
It's a quantity selector field, so depending on input type, the user only has to enter a number or click-increment to 1,2,3,... the number he desires.
Question: Is there a way to switch input type="button" to input type="text" by virtue of a button?
Something like this:
$('.switchInputType').click(function () {
$(this).val() == "click" ? go_type() : go_click();
});
function go_type() {
$('.switchInputType').val('type');
$('.inputElement').attr('type', 'text');
}
function go_click() {
$('.switchInputType').val('click');
$('.inputElement').attr('type', 'button');
}
This doesn't work? I'm looking for other ideas how to achieve this?
Would it be possible tampering with the JQM generated HTML, as
<input type="button" value="clickMe" />
in JQM looks like this:
<div class="ui-btn ui-btn-inline ui-btn-corner-all ui-shadow ui-btn-up-c"
data-theme="c" aria-disabled="false">
<span class="ui-btn-inner ui-btn-corner-all">
<span class="ui-btn-text">clickMe</span>
</span>
<input class="inputElement ui-btn-hidden" type="button" value="clickMe"
aria-disabled="false" />
</div>
Thanks for thoughts!
here is the complete code for switching input in Jquery Mobile:
$('.switchInputType').click(function () {
$(this).val() == "click" ? go_type() : go_click();
});
function go_type() {
$('.switchInputType').val('type');
$('.switchInputType').prev('span').html('<span class="ui-btn-text">type</span><span class="ui-icon ui-icon-finger ui-icon-shadow"></span>');
$('.inputElement').each(function(index) {
$(this).parent('div').replaceWith('<input type="text" value="'+this.value+'"
name="'+this.name+'" id="'+this.id+'" value="'+this.value+'" class="inputElement
ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" />');
});
}
function go_click() {
$('.switchInputType').val('click');
$('.switchInputType').prev('span').html('<span class="ui-btn-text">click</span><span
class="ui-icon ui-icon-finger ui-icon-shadow"></span>');
$('.inputElement').each(function(index) {
$(this).replaceWith('<div class="ui-btn ui-btn-inline ui-btn-corner-all ui-shadow
ui-btn-up-c" data-theme="c" aria-disabled="false"><span class="ui-btn-inner ui-btn-
corner-all"><span class="ui-btn-text">'+this.value+'</span></span><input
type="button" value="'+this.value+'" name="'+this.name+'" id="'+this.id+'"
value="'+this.value+'" class="inputElement ui-btn-hidden" data-inline="true"
aria-disabled="false" /></div>');
});
}