jquery mobile popup not working (for loop)? - jquery-mobile

I tried to create pop ups using for-loop, but does not work. Why?
http://jsfiddle.net/edgardaraujo/Rud3G/2/
I thank you all for the help.
content html
<div data-role="page" id="page1">
<div data-role="content">
<div id="output"></div>
</div>
<!-- /content -->
</div>
content jquery
var array = new Array();
array[0] = "link-image-1";
array[1] = "link-image-2";
array[2] = "link-image-3";
var len = Object.keys(array).length;
for (i = 0; i < len; i++) {
var popup = array[i];
$('#output').css({'font-weight': 'bold'}).append('<img class="popphoto" src="' + popup + '" alt="photo, test" style="width:30%">');
var arrayPhotos = $('<div data-role="popup" id="' + popup + '" data-overlay-theme="a" data-theme="d" data-corners="false"></div>');
arrayPhotos.append('Close <img class="popphoto" src="' + popup + '" style="max-height:512px;" alt="photo, test">');
}

Upadate
When you create [data-role=popup] dynamically, you need to enhance its markup by using .trigger('create').
Demo
You have a mistake in href of the link to popup. You have used variable popup which holds image links instead of using the count variable i.
href="#popup' + i + '"
Here
$('#output').css({ 'font-weight': 'bold'}).append('<img class="popphoto" src="' + popup + '" alt="photo, test" style="width:30%">');
And
var arrayPhotos = $('<div data-role="popup" id="popup' + i +'" data-overlay-theme="a" data-theme="d" data-corners="false"></div>');

Related

jquery mobile fieldcontain css error

I've added some selectboxes to data-role fieldcontain, but even though the append functions are correct, jquery mobile doesn't show properly the selectboxes.
JS:
var container = $("#addprogram").find(".addprogramlist");
container.empty();
// alert(eArray);
for(var i = 1; i <=7; i++)
{
var day = getDay(i);
container.append("<label for='day-" + i + "' class='select'>" + day + "</label><select name='day-" + i + "' id='day-" + i + "'>");
for (var j = 0; j < eArray.length; j++)
{
container.append("<option value='" + eArray[j] + "'>" + eArray[j] + "</option>");
}
container.append("</select>");
}
HTML:
<div data-role="page" id="addprogram">
<div data-role="header" data-position="fixed">
<h1>Add Program</h1>
Back
</div><!-- /header -->
<div data-role="content" class='addprogramcontent'>
<div data-role="fieldcontain" class='addprogramlist'>
</div>
</div>
</div><!-- /page -->
I don't know if I understood your problem correctly, but this might be the answer:
try refreshing your selectmenus as follows:
$("select").selectmenu();
working example http://jsfiddle.net/PG2bV/54/
EDIT
I changed a little your code. That is how I usually do to build a select.

jquery mobile button refresh and multiple binding

I've a problem with Jquery Mobile. Buttons that I add from JS are not displayed properly, and lacks CSS. On the other hand, hitting a button calls that function, but if another button is clicked, because of on off tap, problem occurs. "addExerciseButton" lacks CSS, and the problem occurs within addExerciseButton
<div data-role="page" id="addprogram">
<div data-role="header" data-position="fixed">
<h1>Add Program</h1>
Back
Save
</div><!-- /header -->
<div data-role="content" class='addprogramcontent'>
<div data-role="fieldcontain" class='addprogramlist'>
</div>
</div>
</div><!-- /page -->
JS:
$(document).off("tap").on('tap', '.addExerciseButton', function(event){
//alert(1);
var container = $(this).attr('id');
alert(container);
});
JS page:
eArray.sort();
var container = $("#addprogram").find(".addprogramlist");
container.empty();
// alert(eArray);
for(var i = 1; i <=7; i++)
{
var day = getDay(i);
container.append("<label for='day-" + i + "' class='select'>" + day + "</label>");
var select = $("<select name='day-" + i + "' id='day-" + i + "'></select>");
for (var j = 0; j < eArray.length; j++)
{
select.append("<option value='" + eArray[j] + "'>" + eArray[j] + "</option>");
}
container.append(select);
var addExerciseButton = "<input type='button' value='Add Exercise' class='addExerciseButton' data-role='button' data-theme='b' id='day-" + i + "'/>"
container.append(addExerciseButton);
}
$("select").selectmenu();
$("#day-1").change(function() {
// alert(value);
$("#day-1 option:selected").each(function () {
// alert(1);
var value = $(this).val();
$(this).parent().selectmenu('disable');
alert(value);
});
});
});
you have to refresh buttons after you append em to the dom:
working example: http://jsfiddle.net/PG2bV/55/
CODE
$(":button").button();
Using
container.trigger('create');
solved my problem with no CSS on buttons.

JQuery UI Tabs Dynamically Add Tab

Here's my fiddle - http://jsfiddle.net/TTBzk/
I want to click the add tab button and have it automatically add a tab with prechosen content without a dialog window as seen on JQuery UI's manipulation example here - http://jqueryui.com/tabs/#manipulation
I don't know what I'm doing wrong. Any help would be greatly appreciated.
JQuery
$(function() {
var websiteframe = '<iframe src="browser.html" width="100%" height="100%" allowtransparency="true" frameBorder="0">Your browser does not support IFRAME</iframe>';
var tabs = $("#tabs").tabs();
tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
tabCounter = 2;
function addTab() {
var label = tabTitle.val() || "" + tabCounter,
id = "tabs-" + tabCounter,
li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
websiteframe = '<iframe src="browser.html" width="100%" height="100%" allowtransparency="true" frameBorder="0">Your browser does not support IFRAME</iframe>';
tabs.find(".ui-tabs-nav").append(li);
tabs.append("<div align='center' id='" + id + "'>" + websiteframe + "</div>");
tabs.tabs("refresh");
tabCounter++;
}
$("#add_tab").click(function() {
addTab();
});
});
HTML
<div id="tabs">
<ul>
<li>Home <span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>
<li style="float:right;"><a id="add_tab">+</a></li>
</ul>
<div id="tabs-1">
<iframe src="browser.html" width="100%" height="100%" allowtransparency="true" frameBorder="0">
Your browser does not support IFRAME's
</iframe>
</div>
</div>
CSS
#tabs li .ui-icon-close {
float:left;
margin:0.4em 0.2em 0 0;
cursor:pointer;}
#add_tab {
cursor:pointer;}
div#tabs {
position:absolute;
top:0px;
left:50%;
width:98%;
height:98%;
margin-left:-49.5%;}
div#tabs div {
display:inline-block;
padding:0px;
width:100%;
height:90%;}
In your addTab function you use this line:
var label = tabTitle.val() || "" + tabCounter;
but you never declare a variable with the name tabTitle
Updated jsfiddle
Changes:
<li>
Home
...
var tabTitle = $('#tab_title');
Gave it an ID for testing purpose.
Declared variable.
Tabs get now added dynamically. You should of course change the tab headings name. For the <a href> value use .text() and e.g. add tabCounter to it so it becomes Home2 etc.

Adding content dynamically in JQuery Mobile

I'm using JQuery Mobile 1.2.0 version.
I want to put some html code in this context:
<div id="div-primeros" data-role="collapsible" data-theme="a" data-content-theme="c" data-collapsed="false">
<h2>Primeros</h2>
[I WANT TO ADD CODE DYNAMICALLY HERE]
</div>
And the code I want to add is:
<div class="ui-grid-a">
<div class="ui-block-a" style="width:93%">
<ul data-role="listview" style="width:100%">
<li id="4" data-icon="false">
<a href="index.html">
<img src="some.jpg"></img>
<h3>some text</h3>
<span class="ui-li-count">some text</span>
</a>
</li>
</ul>
</div>
<div class="ui-grid-b" style="width:7%">
<input cant="1" type="number" value="0" min="0" max="8" style="width:100%">
</div>
</div>
So I tried doing this using this code ("primero" is a JSON variable):
$("#div-primeros").append(
"<div id=\"" + i + "\" data-role=\"listview\" class=\"ui-grid-a\">" +
"<div class=\"ui-block-a\" style=\"width:95%\">" +
"<ul style=\"width:100%\">" +
"<li id=\"" + primero.id + "\" data-icon=\"false\">" +
"<a href=\"menu_detalle.html\">" +
"<img src=\"" + primero.thumb + "\"></img>" +
"<h3>" + primero.nombre + "</h3>" +
"<p>" + primero.descripcion + "</p>" +
"<span class=\"ui-li-count\">" + primero.precio + "</span>" +
"</a>" +
"</li>" +
"</ul>" +
"</div>" +
"<div class=\"ui-grid-b\" style=\"width:5%\">" +
"<input cant=\"" + primero.id + "\" type=\"number\" value=\"0\" min=\"0\" max=\"8\" style=\"width:100%\">" +
"</div>" +
"</div>");
The result is that the content not formatted correctly, and this content is out of collapsible "div-primeros".
What am I doing wrong??? I've tried with:
$("div-primeros").trigger("create");
and
$("div-primeros").page();
with no successful results.
Anyone knows how can I refresh all the code?
Thank you so much!!!
Bye
Don't break it up in multiple lines, use single quotes in place of the double quotes surrounding the html you want to append. Like so:
$('#div-primeros').append('<div class="ui-grid-a"><div class="ui-block-a" style="width:93%"><ul data-role="listview" style="width:100%"><li id="4" data-icon="false"><img src="some.jpg"></img><h3>some text</h3><span class="ui-li-count">some text</span></li></ul></div><div class="ui-grid-b" style="width:7%"><input cant="1" type="number" value="0" min="0" max="8" style="width:100%"></div></div>');
Here's a fiddle. http://jsfiddle.net/wU6pr/

JqueryMobile navigation Title problem

here is the html file which calls the above method. When call "callAjax" is clicked, i can get the listview displayed. These listview when clicked will not show navigation bar title.
<body>
<div data-role="page" >
<div data-role="header" data-theme="b" >
<h1>NDUS Directory</h1>
</div><!-- /header -->
<div data-role="content" >
<div id ="divsearch" class ="LogoImage" >
<img src="Images/logo.gif" align="middle" />
</div>
<p></p>
<label for="fname">First Name: </label>
<input type="text" name="fname" id="fname" value="" />
<label for="lname">Last Name: </label>
<input type="text" name="lname" id="lname" value="" />
<p></p>
<input id="callAjax" type="button" value="Search" data-theme="b" />
<!-- TO SHOW PEOCESSING LAG INFORMATION -->
<span id="sp" class = "spanRed"></span>
<div id="resultLog">
</div>
<div id="ajaxBusy" class ="busyAJAX">
<p>
<img src="Images/progress.gif">
</p>
</div>
<span id="placeholder"></span>
<ul id = "idul" data-role="listview" data-theme="b" data-inset="true">
</ul>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
here is a sample ajax call which dynamically generates the listview. The code does its job..but the window which shows the details about the listview(when clicked) does not get any title for the navigation bar. In code..it needs to display the txt.firstname and txt.lastname.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "DirectoryData.asmx/TestSearch",
data: argument,
dataType: "json",
beforeSend: function () {
$("#resultLog").html("Loading" + '<img src="Images/progress.gif" />');
},
success: function (msg) {
var items = [];
$("#unfinshed").remove();
var public_tweets = JSON.parse(msg.d);
if (public_tweets.length > 0) {
// remove older values
$("#placeholder").after('<ol id="unfinshed" data-role="listview" data-theme="b" data-inset="true" data-dividertheme="c"></ol>');
$('<li data-role="list-divider">Records found: ' + public_tweets.length + '</li>').appendTo("#unfinshed");
// $('#unfinshed').empty();
for (var x = 0; x < public_tweets.length; x++) {
var txt = public_tweets[x];
var imageName;
if (txt.type == "Faculty") {
imageName = "Images/BusinessPerson.png";
} else {
imageName = "Images/StudentPerson.png";
}
//<img src="images/gf.png" alt="France" class="ui-li-icon">
$('<li><img src="' + imageName + '" class="ui-li-icon">
<a href="#" >' + txt.firstname + ' ' + txt.lastname + '
</a><p></p><p>' + txt.title + '</p>' +
'<ul data-theme="c" data-inset="true">' +
'<li><span class="dirHeaderFont">' + 'Institution:</span><p></p>
<span class="footerTextFont">' + txt.institution + '</span></li>' +
'<li><span class="dirHeaderFont">' + 'Department:</span><p></p>
<span class="footerTextFont">' + txt.department + '</span></li>' +
'<li><span class="dirHeaderFont">' + 'Title:</span><p></p>
<span class="footerTextFont">' + txt.title + '</span></li>' +
'<li data-icon="grid"><span class="dirHeaderFont">' +
'Phone:</span><p></p><span class="footerTextFont">
<a href="tel:' + txt.phonenumber + '">' + txt.phonenumber +
'</a></span></li>' +
'<li data-icon="search"><span class="dirHeaderFont">' +
'Email:</span><p></p><span class="footerTextFont">
<a href="mailto:' + txt.email + '">' + txt.email +
'</a></span></li>' +
'<li><span class="dirHeaderFont">' + 'Active:</span><p>
</p><span class="footerTextFont">' + txt.active +
'</span></li>' + '</ul>' +
'</li>').appendTo("#unfinshed");
}
$("#unfinshed").page();
} else {
// display no records found
$("#placeholder").after('<ul id="unfinshed" data-role="listview" data-theme="c" data-inset="true"></ul>');
$('<li>' + 'No records found' + '</li>').appendTo("#unfinshed");
$("#unfinshed").page();
} // end of public_tweets check
$("#resultLog").html(' '); // remove the loading image
}
}); // end of ajax
$("#resultLog").ajaxError(function (event, request, settings, exception) {
$("#resultLog").html("Error connecting to database. Try again later.");
//$("#resultLog").html("Error connecting to database. Try again later.: " + settings.url + "<br />HTPP Code: " + request.status);
});
You could try to add a custom title like this:
$("div:jqmData(role='header')").prepend('<h1>Custom Title</h1>');
Might need to tinker with it a tad but it works
UPDATE:
Live Example:
http://jsfiddle.net/phillpafford/trdYP/51/
JS:
$("div:jqmData(role='header') > h1").replaceWith('<h1 class="ui-title" tabindex="0" role="heading" aria-level="1">New Title</h1>');
HTML:
<div data-role="page" id="home">
<div data-role="header">
<h1 >Home</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f">
<li data-role="list-divider">Home Page</li>
<li>Page 2</li>
</ul>
</div>
</div>
<!-- Page 2 -->
<div data-role="page" id="page2">
<div data-role="header">
<h1 >Page 2</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f">
<li data-role="list-divider">Home Page</li>
<li>Home Page</li>
</ul>
</div>
</div>

Resources