Check a radio button by default with jQuery - html

in an html page that I use to display text snippets, I use jQuery to filter those that contain just text, text including some url links or both :
JS code :
$(document).ready(function() {
$('input[type=radio]').attr('checked', false);
$(".selection").on("change", ":radio", function() {
var checkedVal = parseInt($(this).filter(":checked").val(), 10);
$(".container").hide().filter(function() {
if (checkedVal === 2) {
return $(this).find('a[href]').length && $(this)
} else if (checkedVal === 1) {
return $(this).find('a[href]').length <= 0 && $(this)
} else {
return $(this)
}
}).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="selection">
</button> Display : <input type="radio" name="check" value="1" /> Texts only | <input type="radio" name="check" value="2" /> Texts w/ links | <input type="radio" name="check" value="-1" checked="checked" /> Texts + Texts w/ links</div>
I would like the "Texts + Texts w/ links" button to be checked by default and this choice to be kept when the page is refreshed.
I don't know how to do this. Any help would be greatly appreciated, thank you.

Instead of unchecking all the buttons, check the one you want to be the default.
$(document).ready(function() {
$('.selection :radio[value=-1]').attr('checked', true);
$(".selection").on("change", ":radio", function() {
var checkedVal = parseInt($(".selection :radio:checked").val(), 10);
$(".container").hide().filter(function() {
if (checkedVal === 2) {
return $(this).find('a[href]').length && $(this)
} else if (checkedVal === 1) {
return $(this).find('a[href]').length <= 0 && $(this)
} else {
return $(this)
}
}).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="selection">
</button> Display : <input type="radio" name="check" value="1" /> Texts only | <input type="radio" name="check" value="2" /> Texts w/ links | <input type="radio" name="check" value="-1" checked="checked" /> Texts + Texts w/ links</div>

Related

Show and hide html elements depending on radio selection

Yes, there are many posts that are kind of similar but not like mine I guess. After struggling for a while I'm not able to come up with a solution. I need help!
I have 4 different criteria:
Resident/Single, Resident/Family, Non-Resident/Single, Non-Resident/Family
For example: if Resident and Single are checked then show that div. The rest goes the same way.
<fieldset>
<ol id="membership">
<li>
<input type="radio" name="resident" value="Resident" checked="checked" /> Resident
<input type="radio" name="resident" value="Non-Resident" /> Non-Resident
</li>
<li>
<input type="radio" name="single" value="Single" checked="checked" /> Single
<input type="radio" name="single" value="Family" /> Family
</li>
</ol>
<div style="display: none;">
<div>Resident/Single</div>
<div>Resident/Family</div>
<div>Non-Resident/Single</div>
<div>Non-Resident/Family</div>
</div>
</fieldset>
This is as far as I was able to go.
$(document).ready(function() {
$('input').change(function() {
if ($('input[value="Resident"]').is(':checked') && $('input[value="Single"]').is(':checked')) {
$('div').show();
} else if ($('input[value="Resident"]').is(':checked') && $('input[value="Family"]').is(':checked')) {
$('div').show();
} else if ($('input[value="Non-Resident"]').is(':checked') && $('input[value="Single"]').is(':checked')) {
$('div').show();
} else if ($('input[value="Non-Resident"]').is(':checked') && $('input[value="Family"]').is(':checked')) {
$('div').show();
} else {
$('div').hide();
}
});
});
This is my repl: https://repl.it/#labanino/Show-based-on-selection#script.js
Instead of using ifs statement to show/hide divs you can simply get the checked values of radio button then just loop through your divs and compare if the .text() is equal to radio values show that divs only .
Demo Code :
$(document).ready(function() {
$(".category div:eq(0)").show() //show 1st div
$('input').change(function() {
//get radio values
var value = $("input[name='resident']:checked").val();
var value1 = $("input[name='single']:checked").val();
console.log(value + "/" + value1)
$(".category div").hide(); //hide all divs
var divss = value + "/" + value1;
//loop through divs
$(".category div").each(function() {
if ($(this).text() === divss) {
$(this).show() //show that div
}
})
});
})
.category > div {
display: none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset>
<ol id="membership">
<li>
<input type="radio" name="resident" value="Resident" checked="checked" /> Resident
<input type="radio" name="resident" value="Non-Resident" /> Non-Resident
</li>
<li>
<input type="radio" name="single" value="Single" checked="checked" /> Single
<input type="radio" name="single" value="Family" /> Family
</li>
</ol>
<div class="category">
<div>Resident/Single</div>
<div>Resident/Family</div>
<div>Non-Resident/Single</div>
<div>Non-Resident/Family</div>
</div>
</fieldset>
Try selecting the divs by name or value and show them while hiding the others.
Check this repl -
https://repl.it/#DavidThomas12/Show-based-on-selection
I believe it fits your use case.

Show DIV only when a specific radio item is selected

I have this code which works without a problem with select options:
HTML
<select onchange="java_script_:show(this.options[this.selectedIndex].value)">
<option value="Apple">Apple</option>
<option value="Orange">Orange</option>
<option value="Bananna">Bananna</option>
</select>
<div id="hiddenDiv" style="display:none">Hidden Question about Oranges</div>
Script
<script>
function show(aval) {
if (aval == "Orange") {
hiddenDiv.style.display='inherit';
} else {
hiddenDiv.style.display='none';
}
}
</script>
Any ideas how can I make this code to work if those were radio options?
Try it like this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radioName" id="Apple"/> Apple <br>
<input type="radio" name="radioName" id="Orange" /> Orange <br>
<input type="radio" name="radioName" id="Bananna" /> Bananna <br>
</form>
<div id="hiddenDiv" style="display:none">Hidden Question about Oranges</div>
<script>
$('#myForm input').on('change', function() {
if ($(this).attr('id') == "Orange") {
hiddenDiv.style.display='inherit';
} else {
hiddenDiv.style.display='none';
}
});
</script>
My solution with JQuery:
$(function () {
$("select").click(function () {
var validOption = $("select > option[value='Orange']");
if (validOption[0].selected) {
$("#hiddenDiv").show();
} else {
$("#hiddenDiv").hidde();
}
});
});

Why my Checkbox change to text

Why my checkbox change to text?
How to make it we tick whatever checkbox the result change? Can anyone fix my coding? In addition, I want my checkbox more tidy. I don't want it so close with each other checkbox.
<head>
<script>
function fungsi() {
var a = document.getElementById("subAnimal1").checked;
var b = document.getElementById("subAnimal2").checked;
var c = document.getElementById("subAnimal3").checked;
var d = document.getElementById("subAnimal4").checked;
var e = document.getElementById("subAnimal5").checked;
if (a && b && c && d && e) {
document.getElementById("subAnimal").innerHTML = "kiv1";
} else if (a && b) {
document.getElementById("subAnimal").innerHTML = "kiv2";
} else if (b && a && c) {
document.getElementById("subAnimal").innerHTML = "kiv13";
} else if (a) {
document.getElementById("subAnimal").innerHTML = "kiv4";
} else {
alert("In progress!");
}
}
</script>
</head>
<body>
<script type="text/javascript">
function ShowHideDiv(Animal) {
var subAnimal = document.getElementById("subAnimal");
subAnimal.style.display = Animal.checked ? "block" : "none";
console.log(Animal.value)
console.log("Text Inside LABEL:" + Animal.parentNode.textContent)
}
</script>
<label for="Animal">
<input type="checkbox" id="Animal" onclick="ShowHideDiv(this)" value="Animal"/> Animal
</label>
<form>
<div id="subAnimal" style="display: none">
<label>
<input type="checkbox" id="subAnimal1" onclick="fungsi()"/>123
</label>
<label>
<input type="checkbox" id="subAnimal2" onclick="fungsi()"/>456
</label>
<label>
<input type="checkbox" id="subAnimal3" onclick="fungsi()"/>789
</label>
<label>
<input type="checkbox" id="subAnimal4" onclick="fungsi()"/>101112
</label>
<label>
<input type="checkbox" id="subAnimal5" onclick="fungsi()"/>KIV
</label>
<p id="subAnimal"></p>
You have duplicate id subAnimal in the page.
<div id="subAnimal" style="display: none">
...
<p id="subAnimal"></p>
here is the modified version:
<head>
<script>
function fungsi() {
var a = document.getElementById("subAnimal1").checked;
var b = document.getElementById("subAnimal2").checked;
var c = document.getElementById("subAnimal3").checked;
var d = document.getElementById("subAnimal4").checked;
var e = document.getElementById("subAnimal5").checked;
if (a && b && c && d && e) {
document.getElementById("subAnimalx").innerHTML = "kiv1";
} else if (a && b) {
document.getElementById("subAnimalx").innerHTML = "kiv2";
} else if (b && a && c) {
document.getElementById("subAnimalx").innerHTML = "kiv13";
} else if (a) {
document.getElementById("subAnimalx").innerHTML = "kiv4";
} else {
alert("In progress!");
}
}
</script>
</head>
<body>
<script type="text/javascript">
function ShowHideDiv(Animal) {
var subAnimal = document.getElementById("subAnimal");
subAnimal.style.display = Animal.checked ? "block" : "none";
console.log(Animal.value)
console.log("Text Inside LABEL:" + Animal.parentNode.textContent)
}
</script>
<label for="Animal">
<input type="checkbox" id="Animal" onClick="ShowHideDiv(this)" value="Animal"/> Animal
</label>
<form>
<div id="subAnimal" style="display: none">
<label>
<input type="checkbox" id="subAnimal1" onclick="fungsi()"/>123
</label>
<label>
<input type="checkbox" id="subAnimal2" onclick="fungsi()"/>456
</label>
<label>
<input type="checkbox" id="subAnimal3" onclick="fungsi()"/>789
</label>
<label>
<input type="checkbox" id="subAnimal4" onclick="fungsi()"/>101112
</label>
<label>
<input type="checkbox" id="subAnimal5" onclick="fungsi()"/>KIV
</label>
</form>
<p id="subAnimalx"></p>
In Both id is duplicate so try below code
<head>
<script>
function fungsi() {
var a = document.getElementById("subAnimal1").checked;
var b = document.getElementById("subAnimal2").checked;
var c = document.getElementById("subAnimal3").checked;
var d = document.getElementById("subAnimal4").checked;
var e = document.getElementById("subAnimal5").checked;
if (a && b && c && d && e) {
document.getElementById("subAnimalLabel").innerHTML = "kiv1";
} else if (a && b) {
document.getElementById("subAnimalLabel").innerHTML = "kiv2";
} else if (b && a && c) {
document.getElementById("subAnimalLabel").innerHTML = "kiv13";
} else if (a) {
document.getElementById("subAnimalLabel").innerHTML = "kiv4";
} else {
alert("In progress!");
}
}
</script>
</head>
<body>
<script type="text/javascript">
function ShowHideDiv(Animal) {
var subAnimal = document.getElementById("subAnimal");
subAnimal.style.display = Animal.checked ? "block" : "none";
console.log(Animal.value)
console.log("Text Inside LABEL:" + Animal.parentNode.textContent)
}
</script>
<label for="Animal">
<input type="checkbox" id="Animal" onclick="ShowHideDiv(this)" value="Animal"/> Animal
</label>
<form>
<div id="subAnimal" style="display: none">
<label>
<input type="checkbox" id="subAnimal1" onclick="fungsi()"/>123
</label>
<label>
<input type="checkbox" id="subAnimal2" onclick="fungsi()"/>456
</label>
<label>
<input type="checkbox" id="subAnimal3" onclick="fungsi()"/>789
</label>
<label>
<input type="checkbox" id="subAnimal4" onclick="fungsi()"/>101112
</label>
<label>
<input type="checkbox" id="subAnimal5" onclick="fungsi()"/>KIV
</label>
<p id="subAnimalLabel"></p>

How to add tooltip in html?

I have the following code, but it doesn't work, can anyone help me out
<form id="myform" action="#">
<h3>Registration Form</h3>
<div id="inputs">
<!-- username -->
<label for="username">Username</label>
<input id="username" title="Must be at least 8 characters."/>
<br />
<!-- password -->
<label for="password">Password</label>
<input id="password" type="password" title="Make it hard to guess." />
<br />
<!-- email -->
<label for="email">Email</label>
<input id="email" title="We won't send you any marketing material." />
<br />
<!-- message -->
<label for="body">Message</label>
<textarea id="body" title="What's on your mind?"></textarea>
<br />
<!-- message -->
<label for="where">Select one</label>
<select id="where" title="Select one of these options">
<option>-- first option --</option>
<option>-- second option --</option>
<option>-- third option --</option>
</select>
<br />
</div>
<!-- email -->
<label>
I accept the terms and conditions
<input type="checkbox" id="check" title="Required to proceed" />
</label>
<p>
<button type="button" title="This button won't do anything">
Proceed
</button>
</p>
</form>
title attribute does the work.
Your code works in chrome, FF and IE 9 and 10
Why not use placeholder instead of title.
Supported overview
In other browsers you can use this javascript:
<script>
var doc = document;
var inputs = doc.getElementsByTagName('input'),
var supportPlaceholder = 'placeholder' in doc.createElement('input');
var placeholder = function(input) {
var text = input.getAttribute('placeholder');
var defaultValue = input.defaultValue;
input.value = text;
input.onfocus = function() {
if (input.value === defaultValue || input.value === text) {
this.value = '';
}
}
input.onblur = function() {
if (input.value === '') {
this.value = text;
}
}
};
if (!supportPlaceholder) {
for (var i = 0, len = inputs.length; i < len; i++) {
var input = inputs[i], text = input.getAttribute('placeholder');
if (input.type === 'text' && text) {
placeholder(input);
}
}
}
</script>
insert title="" attribute, it will work for you.
Try qTip - http://craigsworks.com/projects/qtip/ and see the demo - http://jsfiddle.net/ramsunvtech/zzZBM/

Error loading page while transiting from one page to another in jquery mobile+ Phonegap

I am working on the Android hybrid application using phonegap and JQuery Mobile. My application involves user registration, sign in and payment. The problem I am facing is that when I try to navigate from one page to another I get message error loading page.
I am creating data-role pages and using changepage method to navigate to different page.
Here are the data-role pages and js file where I am getting this error.
First js File bookingSearchResult.js:
var jsonData=new Array();
$(document).ready(function(e) {
$(".radioCheck").live("change",(function(event, ui){
var value=$(this).val();
value=value.split("_");
var str=value[0]+ "(INR"+value[1]+"/-)";
var tempid=this.id;
tempid=tempid.split("_");
$("#spRoomType_"+tempid[2]).html(str);
}));
$("#bookingform").live("pagebeforeshow",function(e){
loadpagedata();
});
$("#btnFormSubmit").live("click",function(){
$.blockUI({ message: '<div class="loading-text">Please wait...</div>' });
var roomsData=$('#selectmenu2').val();
var aduldetails=$('#selectmenu3').val();
for(var i=1;i<=roomsData;i++){
for(var j=1;j<=aduldetails;j++){
var fname=$('#Fname_'+i+'_'+j+'').val();
var lname=$('#Lname_'+i+'_'+j+'').val();
var email=$('#Email_'+i+'_'+j+'').val();
var mobile=$('#Monumber_'+i+'_'+j+'').val();
if(fname=="")
{
$.unblockUI();
jAlert('Please enter First Name','Alert',function(){
$(".valFname").focus();
});
return false;
}
else if(!fname.match(/^[A-Za-z]+$/))
{
$.unblockUI();
jAlert('First Name can have alphabets only','Alert',function(){
$(".valFname").focus();
});
return false;
}
else if(fname.length>15)
{
$.unblockUI();
jAlert('First Name cannot be greater than 15 alphabets','Alert',function(){
$(".valFname").focus();
});
return false;
}
if(lname=="")
{
$.unblockUI();
jAlert('Please enter Last Name','Alert',function(){
$(".valLname").focus();
});
return false;
}
else if(!lname.match(/^[A-Za-z]+$/))
{
$.unblockUI();
jAlert('Last Name can have alphabets only','Alert',function(){
$(".valLname").focus();
});
return false;
}
else if(lname.length>15)
{
$.unblockUI();
jAlert('Last Name cannot be greater than 15 alphabets','Alert',function(){
$(".valLname").focus();
});
return false;
}
if(email=="")
{
$.unblockUI();
jAlert('Please enter Email Address','Alert',function(){
$(".valEmail").focus();
});
return false;
}
else if(!email.match(/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/))
{
$.unblockUI();
jAlert('Enter valid Email Address','Alert',function(){
$(".valEmail").focus();
});
return false;
}
if(mobile=="")
{
$.unblockUI();
jAlert('Please enter Mobile Number','Alert',function(){
$('.valMobile').focus();
});
return false;
}
else if(!mobile.match(/([0-9]{10})$/))
{
$.unblockUI();
jAlert('Enter valid 10-digit Mobile Number','Alert',function(){
$('.Monumber').focus();
});
return false;
}
}
}
$.unblockUI();
$.mobile.changePage("#bookingConf");
});
});
$('#bookingSearchResult').live('pagebeforeshow',function(event){
$('#hotelListDiv').empty();
$('#hotelList').empty();
$('#detailsDiv').empty();
$('#hDisplay').text("Hotels Available in "+$('#Cityname option:selected').text());
var dayWise='';
var priceBreakup='';
var dataRetrieved=new Array();
var dynHotels="";
dataRetrieved=JSON.parse(localStorage['search']);
$.each(dataRetrieved, function (index, status) {
var cinDate=JSON.parse(localStorage['search'])[index].checkInDate;
var checkinDate=displayDate2(cinDate);
$('#topDate').text(checkinDate[0]);
var nights=$('#nights').val();
var rooms=new Array();
var pax=new Array();
var roomDetails='';
var numAdults=new Array();
numAdults[0]=$('#selectmenu3').val();
if($('#selectmenu2').val()>1){
for(var i=1;i<$('#selectmenu2').val();i++){
numAdults[i]=$("#"+"selectmenu3"+i).val();
}
}
for(var i=0;i<$('#selectmenu2').val();i++)
{
roomDetails="<br>"+roomDetails+"Room "+(i+1)+": "+numAdults[i]+" Adult</br>";
}
$.each(this.availabilityList, function (index, status) {
var dynRates='';
var hotel=this.hotelName;
var hotelId=this.hotelId;
var priceString="";
$.each(this.rate, function (index, status) {
var offerPrice=this.price;
var rateId=this.rateId;
var rateDesc=this.rateIdTypeDesc;
var roomVisited=0;
var daySplit='';
var dayWisePrice='';
$.each(this.roomGrid.room, function (index, status) {
if(roomVisited !=this.roomNumber ){
var roomNo=this.roomNumber;
var roomType=this.roomType;
var numOfPax=this.numOfPax;
$.each(this.daywiseRates, function (index, status) {
var dateVisited=0;
$.each(this.forday, function (index, status) {
if(dateVisited !=this.date ){
var day=this.date;
var dayWiseTotal=this.price;
dayWisePrice=dayWisePrice+roomNo+"%"+numOfPax+"_"+day+":"+dayWiseTotal+"#";
dateVisited=this.date;
}
});
});
}
roomVisited=this.roomNumber;
});
var buttonId="btnBooknow_"+hotelId+"_"+rateId;
var priceBreakupId="priceBreakupText$"+hotelId+"$"+rateId+"$"+dayWisePrice;
dynRates=dynRates+'<li class="pricebreakup"><div class="pricebreakup-strip">'+rateDesc+' ₹ '+offerPrice+'/-<br><span>(Lux. Tax Excl.)</span></div> <span class="priceBreak" id='+priceBreakupId+'>Price Breakup</span> <div class="submit-btn-wrap"><input name="Booknow" type="button" class="button-bg" id='+buttonId+' value="BOOK NOW"/></div></li>';
});//end of rate
dynHotels=dynHotels+'<div class="booking_search_result_hotel_item_wrap"><a id="info_popup" href="#info_popup" data-rel="dialog" class="info_btn"><img src="images/i_ico.png" width="18" height="18" alt="Info"></a><div data-role="collapsible" id="hotelList" data-collapsed="true"><h3 id="hName">'+hotel+'</h3><ul class="form-list-item booking_search_result"><li class="booking_terms">Service tax # 7.42% will be charged (As per new notification).</li><li class="check-in-details"><a id="policy_popup" href="#policy_popup" data-rel="dialog" class="info_btn"><img src="images/p_ico.png" width="27" height="26" alt="i_ico" class="i_ico"></a> Check in: '+checkinDate[1]+', '+nights+' Nights</span><br>'+roomDetails+'</li>'+dynRates+'</ul></div></div> ';
});//end of availabilityList
});
$(dynHotels).appendTo('#hotelListDiv');
$('div[data-role=collapsible]').collapsible({refresh:true});
$('input[type=button]').button({refresh:true});
$('input[name="Booknow"]').click(function(){
var btnId=this.id.split('_');
hotelIdSelected=btnId[1];
rateIdSelected=btnId[2];
$.mobile.changePage('#bookingform');
});
$('.priceBreak').click(function(){
var id=this.id;
if(typeof(Storage)!=="undefined")
{
localStorage.priceBreakId=id;
}
$.mobile.changePage('#priceBreakup');
});
});
Second js File : bookingGuestDetails.js
$(document).ready(function(){
$("#bookingConf").live("pagebeforeshow",function(e){
loadBookingConfData();
});
$(".edit-btn1").live("click",function(){
var imgId=this.id;
imgId=imgId.split('_');
var str=imgId[1];
$("#ulBookingConf li").empty();
loadBookingConfDataForEdit();
});
$("#btnSubmitConf").live("click",function(){
$.blockUI({ message: '<div class="loading-text">Please wait...</div>' });
var conFname=$(".clsConName").val();
var conEmail=$(".clsConEmail").val();
var conMobile=$(".clsConMobile").val();
if(conFname=="")
{
$.unblockUI();
jAlert('Please enter First Name','Alert',function(){
$(".clsConName").focus();
});
return false;
}
else if(!conFname.match(/^[a-zA-Z ]*$/))
{
$.unblockUI();
jAlert('First Name can have alphabets only','Alert',function(){
$(".clsConName").focus();
});
return false;
}
else if(conFname.length>15)
{
$.unblockUI();
jAlert('First Name cannot be greater than 15 alphabets','Alert',function(){
$(".clsConName").focus();
});
return false;
}
if(conEmail=="")
{
$.unblockUI();
jAlert('Please enter Email Address','Alert',function(){
$(".clsConEmail").focus();
});
return false;
}
else if(!conEmail.match(/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/))
{
$.unblockUI();
jAlert('Enter valid Email Address','Alert',function(){
$(".clsConEmail").focus();
});
return false;
}
if(conMobile=="")
{
$.unblockUI();
jAlert('Please enter Mobile Number','Alert',function(){
$('.clsConMobile').focus();
});
return false;
}
else if(!conMobile.match(/([0-9]{10})$/))
{
$.unblockUI();
jAlert('Enter valid 10-digit Mobile Number','Alert',function(){
$('.clsConMobile').focus();
});
return false;
}
$.unblockUI();
createProvisional();
});
});
function loadBookingConfData(){
var noOfRoom=$('#selectmenu2').val();
var nights=$('#nights').val();
var noOfAdults=0;
var cinDate=JSON.parse(localStorage['search'])[0].checkInDate;
var displayDate=displayDate2(cinDate)[1];
var roomDetails='<li class="booking_full_guest_head"><ul id="booking_full_guest_head_ul"><li>Hotel <span id="hotelConf">'+localStorage.hotelNameGuestDetails+'</span></li><li>Check-in <span id="CheckinConf">'+displayDate+'</span></li><li>Nights <span id="NightsConf">'+nights+'</span></li></ul></li>';
for(var i=1;i<=noOfRoom;i++)
{
roomDetails+='<li><div class="booking_full_guest_head_edit">Room-'+i+'<br>'+$("#spRoomType_"+i).html()+'<img class="edit-btn1" id="imgEdit_'+i+'" src="images/edit-ico.jpg" width="19" height="18" alt="Edit"></div></li> ';
if(i!=1){
noOfAdults=$('#selectmenu3'+(i-1)).val();
}
else
{
noOfAdults=$('#selectmenu3').val();
}
for(var j=1;j<=noOfAdults;j++){
if(document.getElementById('Gender_'+i+'_'+j+'_0').checked)
{
roomDetails+='<li class="bookinsg_full_guest_adult_seprator"><div class="booking_full_guest_type_head"> Adult '+j+'</div><div data-role="fieldcontain"> <label for="textinput">Name </label> <input class="enFields_'+ i +'" disabled="disabled" name="textinput" type="text" id="Fname1_'+i+'_'+j+'" value="'+$("#Fname_"+i+"_"+j).val()+' '+$("#Lname_"+i+"_"+j).val()+'" /></div><div data-role="fieldcontain" class="radio-input-wrap"><fieldset data-role="controlgroup" data-type="horizontal"><label class="gender-label">Gender<span class="mandatory-gender-sign">*</span></label><input class="enFields_'+ i +'" disabled="disabled" name="Gender1_'+i+'_'+j+'" type="radio" id="Gender1_'+i+'_'+j+'_0" value="" checked /><label for="Gender1_'+i+'_'+j+'_0">Male</label><input class="enFields_'+ i +'" disabled="disabled" type="radio" name="Gender1_'+i+'_'+j+'" id="Gender1_'+i+'_'+j+'_1" value="" /> <label for="Gender1_'+i+'_'+j+'_1">Female</label></fieldset></div><div data-role="fieldcontain"><label for="textinput">Email </label><input class="enFields_'+ i +'" name="textinput" disabled="disabled" type="email" id="Email1_'+i+'_'+j+'" value="'+$("#Email_"+i+"_"+j).val()+'" /></div> <div data-role="fieldcontain"><label for="textinput">Number </label><input class="enFields_'+ i +'" name="textinput" type="number" id="Monumber1_'+i+'_'+j+'" disabled="disabled" value="'+$("#Monumber_"+i+"_"+j).val()+'" /> </div></li>';
}
else if(document.getElementById('Gender_'+i+'_'+j+'_1').checked)
{
roomDetails+='<li class="bookinsg_full_guest_adult_seprator"><div class="booking_full_guest_type_head"> Adult '+j+'</div><div data-role="fieldcontain"> <label for="textinput">Name </label> <input class="enFields_'+ i +'" disabled="disabled" name="textinput" type="text" id="Fname1_'+i+'_'+j+'" value="'+$("#Fname_"+i+"_"+j).val()+' '+$("#Lname_"+i+"_"+j).val()+'" /></div><div data-role="fieldcontain" class="radio-input-wrap"><fieldset data-role="controlgroup" data-type="horizontal"><label class="gender-label">Gender<span class="mandatory-gender-sign">*</span></label><input class="enFields_'+ i +'" disabled="disabled" name="Gender1_'+i+'_'+j+'" type="radio" id="Gender1_'+i+'_'+j+'_0" value="" /><label for="Gender1_'+i+'_'+j+'_0">Male</label><input class="enFields_'+ i +'" disabled="disabled" type="radio" name="Gender1_'+i+'_'+j+'" id="Gender1_'+i+'_'+j+'_1" value="" checked/> <label for="Gender1_'+i+'_'+j+'_1">Female</label></fieldset></div><div data-role="fieldcontain"><label for="textinput">Email </label><input class="enFields_'+ i +'" name="textinput" type="email" id="Email1_'+i+'_'+j+'" value="'+$("#Email_"+i+"_"+j).val()+'" disabled="disabled"/></div> <div data-role="fieldcontain"><label for="textinput">Number </label><input class="enFields_'+ i +'" name="textinput" type="number" id="Monumber1_'+i+'_'+j+'" disabled="disabled" value="'+$("#Monumber_"+i+"_"+j).val()+'" /> </div></li>';
}
}
}
var netCost="";
netCost=calcTotalResevationCost();
roomDetails+='<li class="booking_full_guest_adult_seprator booking_full_guest_adult_total">Total Cost INR '+ netCost +' /-</li><li class="submit-btn-wrap"><input name="Submit" type="submit" class="button-bg" id="btnCreateProv" value="Save"/><br><input name="Reset" type="reset" value="Cancel" class="button-bg"/></li>';
$("#ulBookingConf").empty();
$(roomDetails).appendTo("#ulBookingConf").trigger("create");
}
function loadBookingConfDataForEdit(){
var cinDate=JSON.parse(localStorage['search'])[0].checkInDate;
var displayDate=displayDate2(cinDate)[1];
var nights=$('#nights').val();
var noOfRoom=$('#selectmenu2').val();
var noOfAdults=0;
var roomDetails='<li class="booking_full_guest_head"><ul id="booking_full_guest_head_ul"><li>Hotel <span id="hotelConf">'+localStorage.hotelNameGuestDetails+'</span></li><li>Check-in <span id="CheckinConf">'+displayDate+'</span></li><li>Nights <span id="NightsConf">'+nights+'</span></li></ul></li>';
for(var i=1;i<=noOfRoom;i++)
{
roomDetails+='<li id="liConfPage"><div class="booking_full_guest_head_edit">Room-'+i+'<br>'+$("#spRoomType_"+i).html()+'<img class="edit-btn1" id="imgEdit_'+i+'" src="images/edit-ico.jpg" width="19" height="18" alt="Edit"></div></li> ';
if(i!=1){
noOfAdults=$('#selectmenu3'+(i-1)).val();
}
else
{
noOfAdults=$('#selectmenu3').val();
}
for(var j=1;j<=noOfAdults;j++){
if(document.getElementById('Gender_'+i+'_'+j+'_0').checked)
{
roomDetails+='<li id="liConfPageGender" class="bookinsg_full_guest_adult_seprator"><div class="booking_full_guest_type_head"> Adult '+j+'</div><div data-role="fieldcontain" id="guestDetails"> <label for="textinput">Name </label> <input class="clsConName" name="textinput" type="text" id="Fname1_'+i+'_'+j+'" value="'+$("#Fname_"+i+"_"+j).val()+' '+$("#Lname_"+i+"_"+j).val()+'" /></div><div data-role="fieldcontain" class="radio-input-wrap"><fieldset data-role="controlgroup" data-type="horizontal"><label class="gender-label">Gender<span class="mandatory-gender-sign">*</span></label><input type="radio" class="clsConRadio" name="Gender1_'+i+'_'+j+'" id="Gender1_'+i+'_'+j+'_0" value="" checked /><label for="Gender1_'+i+'_'+j+'_0">Male</label><input type="radio" class="enFields_'+ i +'" name="Gender1_'+i+'_'+j+'" id="Gender1_'+i+'_'+j+'_1" value="" /> <label for="Gender1_'+i+'_'+j+'_1">Female</label></fieldset></div><div data-role="fieldcontain"><label for="textinput">Email </label><input class="clsConEmail" name="textinput" type="text" id="Email1_'+i+'_'+j+'" value="'+$("#Email_"+i+"_"+j).val()+'" /></div> <div data-role="fieldcontain"><label for="textinput">Number </label><input type="text" class="clsConMobile" name="textinput" id="Monumber1_'+i+'_'+j+'" value="'+$("#Monumber_"+i+"_"+j).val()+'" /> </div></li>';
}
else if(document.getElementById('Gender_'+i+'_'+j+'_1').checked)
{
roomDetails+='<li class="bookinsg_full_guest_adult_seprator" id="liConfpagefulldetails"><div class="booking_full_guest_type_head"> Adult '+j+'</div><div data-role="fieldcontain"> <label for="textinput">Name </label> <input type="text" class="clsConName" name="textinput" id="Fname1_'+i+'_'+j+'" value="'+$("#Fname_"+i+"_"+j).val()+' '+$("#Lname_"+i+"_"+j).val()+'" /></div><div data-role="fieldcontain" class="radio-input-wrap"><fieldset data-role="controlgroup" data-type="horizontal"><label class="gender-label">Gender<span class="mandatory-gender-sign">*</span></label><input type="radio" class="enFields_'+ i +'" name="Gender1_'+i+'_'+j+'" id="Gender1_'+i+'_'+j+'_0" value="" /><label for="Gender1_'+i+'_'+j+'_0">Male</label><input type="radio" class="enFields_'+ i +'" name="Gender1_'+i+'_'+j+'" id="Gender1_'+i+'_'+j+'_1" value="" checked/> <label for="Gender1_'+i+'_'+j+'_1">Female</label></fieldset></div><div data-role="fieldcontain"><label for="textinput">Email </label><input type="text" class="clsConEmail" name="textinput" id="Email1_'+i+'_'+j+'" value="'+$("#Email_"+i+"_"+j).val()+'" /></div> <div data-role="fieldcontain"><label for="textinput">Number </label><input type="text" class="clsConMobile" name="textinput" id="Monumber1_'+i+'_'+j+'" value="'+$("#Monumber_"+i+"_"+j).val()+'" /> </div></li>';
}
}
}
var netCost="";
netCost=calcTotalResevationCost();
roomDetails+='<li class="booking_full_guest_adult_seprator booking_full_guest_adult_total">Total Cost INR '+ netCost +' /- </li><li class="submit-btn-wrap"><input name="Submit" type="submit" class="button-bg" id="btnSubmitConf" value="Save"/><br><input name="Reset" type="reset" value="Cancel" class="button-bg"/></li>';
$("#ulBookingConf").empty();
$(roomDetails).appendTo("#ulBookingConf").trigger("create");
}
Follows is our data-role pages for booking, one is bookingform and other is bookingConfirmationPage
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Ginger</title>
<link href="css/dark-theme.min.css" rel="stylesheet" type="text/css"/>
<!--<link href="css/jquery-ui.css" rel="stylesheet" type="text/css"/> -->
<link href="css/mystyle.css" rel="stylesheet" type="text/css"/>
<link href="css/jquery.mobile.structure-1.2.0.css" rel="stylesheet" type="text/css" />
<link href="css/jalerts.css" rel="stylesheet" type="text/css"/>
<!-- Includes Mobiscroll -->
<link href="css/mobiscroll-2.3.1.custom.min.css" rel="stylesheet" type="text/css" />
</head>
<body class="booking-bg" id="gingerAppBody">
<div data-role="page" id="booking" data-theme="a" class="form-content-wrap home-bg">
<div data-role="header" data-id="ginger_header" data-position="fixed">
<h1>BOOKING</h1>
<!------------- booking form page --------------------->
<div data-role="page" id="bookingform" data-theme="a" class="form-content-wrap">
<div data-role="header" data-id="ginger_header" data-position="fixed">
<h1>BOOKING</h1>
Back
Call
Menu
</div>
<form method="get">
<div data-role="content" class="form-content-wrap" >
<div data-role="collapsible-set" class="booking_form_wrap" id="roomListDiv"></div>
<ul class="form-list-item">
<li class="booked-by-head">
Booked By...
</li>
<li>
<div data-role="fieldcontain">
<select name="flipswitch3" id="flipswitch3" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
<label for="flipswitch3">Booker is same as guest 1</label>
</div>
</li>
<li>
<div data-role="fieldcontain">
<input type="text" name="Fname" id="BookerFname" value="" placeholder="First Name" />
</div>
<span class="mandatory-sign">*</span>
</li>
<li>
<div data-role="fieldcontain">
<input type="text" name="Lname" id="BookerLname" value="" placeholder="Last Name" />
</div>
<span class="mandatory-sign">*</span>
</li>
<li class="radio-input-li">
<div data-role="fieldcontain" class="radio-input-wrap">
<fieldset data-role="controlgroup" data-type="horizontal">
<label class="gender-label">Gender
<span class="mandatory-gender-sign">*</span></label>
<input name="BookerGender" type="radio" id="BookerGender_0" value=""/>
<label for="BookerGender_0">Male</label>
<input type="radio" name="BookerGender" id="BookerGender_1" value="" />
<label for="BookerGender_1">Female</label>
</fieldset>
</div>
</li>
<li>
<div data-role="fieldcontain">
<input type="email" name="Email" id="BookerEmail" value="" placeholder="Email" />
</div>
<span class="mandatory-sign">*</span>
</li>
<li>
<div data-role="fieldcontain">
<input type="tel" name="Monumber" id="BookerMonumber" value="" placeholder="Mobile Number" />
</div>
<span class="mandatory-sign">*</span>
</li>
<li>
<div data-role="fieldcontain">
<select name="flipswitch2" id="flipswitch2" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
<label for="flipswitch2">Subscribe to the 'Ginger' newsletter</label>
</div>
</li>
<li class="submit-btn-wrap">
<input name="Submit" type="submit" value="Submit" id="btnFormSubmit" class="button-bg"/>
<br>
<input name="Reset" type="reset" value="Reset" class="button-bg"/>
</li>
</ul>
</div>
</form>
</div>
<!------------- booking confirmation page --------------------->
<div data-role="page" id="bookingConf" data-theme="a" class="form-content-wrap">
<div data-role="header" data-id="ginger_header">
<h1>BOOKING</h1>
Back
Call
Menu
</div>
<form method="get">
<div data-role="content" class="form-content-wrap" >
<ul id="ulBookingConf" class="form-list-item booking_payment">
</ul>
</div>
</form>
</div>
</div>
<script src="js/head.min.js"></script>
<script>
head.js("js/jquery-1.8.2.min.js", "cordova-2.1.0.js", "js/jquery-ui.min.js", "js/jquery.blockUI-min.js","js/jquery.alerts.min.js","jquery.mobile/jquery.mobile-1.2.0.min.js","js/mobiscroll-2.3.1.custom.js","js/registration.js","js/booking.js","js/bookingSearch.js","js/bookingSearchResult.js","js/bookingGuestDetails.js","js/paymentSuccess.js","js/priceBreakup.js","js/common.js",function(){
//head.js("js/jquery-1.8.2.min.js", "cordova-2.1.0.js", "js/jquery-ui.min.js", "js/jquery.blockUI-min.js","js/jquery.alerts.min.js","jquery.mobile/jquery.mobile-1.2.0.min.js","js/mobiscroll-2.3.1.custom.js","js/default.js","js/registration1.js","js/booking1.js","js/paymentSuccess.js",function(){
localStorage.clear();
$.mobile.selectmenu.prototype.options.nativeMenu = false;
$.mobile.phonegapNavigationEnabled = true ;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#booking')){
e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory();
}
}, false);
}
});
</script>
</body>
</html>
With jquery mobile this
$(document).ready(function(){
is not needed (actually is wrong to use it like this. See here.)
When you navigate to a page, a series of events is being fired.
pagebeforecreate, pagecreate, pagebeforeshow, pageshow etc are some of
them. You bind to those events not document ready. Since you use
phonegap you should see the deviceready event and mobileinit.