Using jQuery load html forms dynamically using append function. Here the following code load the page content dynamically based on number times of values on while loop.
Here I have a struggle on load the content with different values.its working with single value of 0 or 1 on var load_with_value=0; but not on both simultaneously i.e. increment the load_with_value++ for again load the page content of HTML forms.
$(document).ready(function(e) {
$("<DIV>").load("<?php echo $url; ?>", function() //url for loading page
{
var n = $('.item').length + 1; //load the html page content
var i = 1; //iteration for number of times load the content
var count = 2; //check the condition
var load_with_value = 0; //load the page content with different values for display different values on html form
while(i<count) { //loop starts
$("#product").append($(this).html());
i++;
load_with_value++;
}
});
});
First of all let's do some proper code formatting and get rid of the incorrect comments:
$(document).ready(function(e) {
$("<DIV>").load("<?php echo $url; ?>", function() {
var n = $('.item').length + 1;
var i = 1;
var count = 2;
var load_with_value = 0;
while(i<count) {
$("#product").append($(this).html());
i++;
load_with_value++;
}
});
});
Now let's take it apart:
If you want to use a temporary element to store the loaded data you need to assign it to a variable, so instead of
$("<DIV>").load("<?php echo $url; ?>", function() {
do
var tempObject = $("<div/>").load("<?php echo $url; ?>", function() {
Afterwards you can append the temporary element to an existing one with $('#someExistingElement').append(tempObject).
If you want to load the content into an existing element you should use it's ID, class or other selector to do this - not $("<div>").. If you want to load it to all div elements (please don't) then it should be $("div").
Next var n = $('.item').length + 1; makes no sense. It is never used in the code.
While cycle in this case is unnecessary. Don't use while cycles if you don't have to. You can use:
for(var i=0; i<count; i++){
//code
}
What is var load_with_value = 0; used for? I can only see you incrementing it with load_with_value++; but you don't use it anywhere..
Finally if you want to load different content based on the incremented variable it should be done outside of the .load function.. For example
$(document).ready(function(){
for(var i=0; i<5; i++){
$('#container-' + i).load('/somecontent-' + i + '.html');
}
});
This loads the content /somecontent-0.html to /somecontent-4.html into container elements with IDs container-0 to container-4 respectively.
I am trying to get a PHP variable to send through javascript and I am having trouble. I am using a jQuery popup I found online and when a user clicks an item, I need that item_id variable to be fetchable in the popup where I want to add a form.
My PHP section.
<a href="#?w=500&item_id='.$stock['id'].'" rel="popup1" class="poplight">
and the Javascript.
$(document).ready(function(){
//When you click on a link with class of poplight and the href starts with a #
$('a.poplight[href^=#]').click(function() {
var popID = $(this).attr('rel'); //Get Popup Name
var popURL = $(this).attr('href'); //Get Popup href to define size
//Pull Query & Variables from href URL
var query= popURL.split('?');
var dim= query[1].split('&');
var popWidth = dim[0].split('=')[1]; //Gets the first query string value
var itemID = dim[0].split('=')[2]; //Gets the second query string value
//Fade in the Popup and add close button
$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<img src="/layout/close_pop.png" class="btn_close" title="Close Window" alt="Close" />');
//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
var popMargTop = ($('#' + popID).height() + 80) / 2;
var popMargLeft = ($('#' + popID).width() + 80) / 2;
document.getElementById('popup1').innerHtml = itemID;
//Apply Margin to Popup
$('#' + popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
//Fade in Background
$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer
return false;
});
//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
$('#fade , .popup_block').fadeOut(function() {
$('#fade, a.close').remove();
}); //fade them both out
return false;
});
});
I added what I thought you work, but it does not var itemID = dim[0].split('=')[2]; //Gets the second query string value and document.getElementById('popup1').innerHtml = itemID;
Use a sophisticated function to retrieve URL query parameters:
function getParameterByName(name, locationObject)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(locationObject.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
[Adopted from here]
Simply call it like so:
getParameterByName('item_id', someLink);
Working Fiddle: http://jsfiddle.net/ADpYT/
You have a simple oversight in your original code ..
When you obtain your dim variable, you get an array with 2 items split at & . In the above code you get ["w=500", "item_id=some_value"] .So in order to get the itemID you have to use dim[1].
var itemID=dim[1].split('=')[1];
I have a jQuery function that opens a modal popup and I have in the same file a variable in PHP like $url="iteminfo.php?ID=".$i['itmid'];($i['itmid'] is the id of some products from MySQL). The jQuery function looks like this:
<script type="text/javascript">
$(document).ready(function(Surl) {
var source="iteminfo.php?ID=<?echo $i['itmid']?>";
var width = 920;
var align = "center";
var top = 100;
var padding = 10;
var backgroundColor = "#FFFFFF";
var borderColor = "#000000";
var borderWeight = 4;
var borderRadius = 5;
var fadeOutTime = 300;
var disableColor = "#666666";
var disableOpacity = 40;
var loadingImage = "js/popup/loading.gif";
$(".modal").click(function() {
modalPopup( align,
top,
width,
padding,
disableColor,
disableOpacity,
backgroundColor,
borderColor,
borderWeight,
borderRadius,
fadeOutTime,
source,
loadingImage );
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
closePopup(fadeOutTime);
}
});
});
</script>
It opens the respective links but the function open all of them like in a loop. How can I pass the $url into the jQuery function to open the respective link for the respective product?
For starters it would look more like this, but without seeing your HTML code its hard to say how you would be pulling out the ID of each different object with an ID.
<script type="text/javascript">
$(document).ready(function(Surl) {
var source="iteminfo.php?ID=";
var width = 920;
var align = "center";
var top = 100;
var padding = 10;
var backgroundColor = "#FFFFFF";
var borderColor = "#000000";
var borderWeight = 4;
var borderRadius = 5;
var fadeOutTime = 300;
var disableColor = "#666666";
var disableOpacity = 40;
var loadingImage = "js/popup/loading.gif";
$(".modal").click(function() {
//get the id of what you're opening on each click event.
var myid = ...
modalPopup( align,
top,
width,
padding,
disableColor,
disableOpacity,
backgroundColor,
borderColor,
borderWeight,
borderRadius,
fadeOutTime,
source + myid,
loadingImage );
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
closePopup(fadeOutTime);
}
});
});
</script>
Did you said that when you click in the element .modal, the right modal appear with the good content ?
And what do you mean by 'them' in 'the function open all of them' ?
Maybe you should take a look in your 'modalPopup' function.
You can store the corresponding url for every .modal element as an attribute with a php loop which browse your array.
I am using a popup jquery that pops up a static block of code using a href=#?w=300?h=200 but I need apart from the size to pass a variable from a sql database also a href=?prop_id=$id . I have tried to combine both of them but nothing seems to work does anybody has a clue??
thank you
$(document).ready(function() {
//When you click on a link with class of poplight and the href starts with a #
$('a.poplight[href^=#]').click(function() {
var popID = $(this).attr('rel'); //Get Popup Name
var popURL = $(this).attr('href'); //Get Popup href to define size
//Pull Query & Variables from href URL
var query= popURL.split('?');
var dim= query[1].split('&');
var popWidth = dim[0].split('=')[1]; //Gets the first query string value
//Fade in the Popup and add close button
$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<img src="to_use/close.png" class="btn_close" title="Close Window" alt="Close" />');
//Define margin for center alignment (vertical horizontal) - we add 80px to the height/width to accomodate for the padding and border width defined in the css
var popMargTop = ($('#' + popID).height() + 80) / 2;
var popMargLeft = ($('#' + popID).width() + 80) / 2;
//Apply Margin to Popup
$('#' + popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
//Fade in Background
$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
return false;
});
//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
$('#fade , .popup_block').fadeOut(function() {
$('#fade, a.close').remove(); //fade them both out
});
return false;
});
});
A simpler approach would be to use data- attributes in combination with jQuery's .data() method
HTML
Text
JS
/* in click handler */
var data=$(this).data();
var propId=data.propId;/* same as $(this).data('propId') */
var popWidth=data.w;
/* etc..*/
Does anybody know how to make the text that appears in the following "li" both a link and also customizable through CSS? I have been unable to drop the text-decoration, change font style, color, etc. I've tried changing the style of the "tree" id but I was only able to change font size.
While both are important the link is crucial. Each "li" that is returned needs to be its own dynamically generated link. I've tried about 10 different ways now and I can't quite seem to get it to work.
<script>
function to_ul(id) {
var ul = document.createElement("ul");
for (var i=0, n=id.length; i<n; i++) {
var branch = id[i];
var li = document.createElement("li");
var text = document.createTextNode(branch.trackName);
li.appendChild(text);
ul.appendChild(li);
}
return ul;
}
function renderTree() {
var treeEl = document.getElementById("tree");
var treeObj = {"root":[{"id":"1","trackName":"Whippin Post"},{"id":"2","trackName":"Sweet Caroline"},{"id":"3","trackName":"Tears in Heaven"},{"id":"4","trackName":"Ain't She Sweet"},{"id":"5","trackName":"Octopus' Garden"},{"id":"6","trackName":"Teen Spirit"},{"id":"7","trackName":"Knockin on Heaven's Door"}]};
treeEl.appendChild(to_ul(treeObj.root));
}
</script>
</head>
<body onload="renderTree()">
<div id="tree"></div>
</body>
</html>
UPDATE
<script>
function to_ul(id) {
var ul = document.createElement("ul");
for (var i=0, n=id.length; i<n; i++) {
var branch = id[i];
var li = document.createElement("li");
li.innerHTML = "" + branch.trackName + ""
ul.appendChild(li);
function changeText(){
document.getElementById('player-digital-title').innerHTML = branch.trackFile;
}
}
return ul;
}
function renderTree() {
var treeEl = document.getElementById("player-handwriting-title");
var treeObj = {"root":[{"id":"1","trackName":"Whippin Post","trackFile":"test1.wma"},{"id":"2","trackName":"Sweet Caroline","trackFile":"test2.wma"},{"id":"3","trackName":"Tears in Heaven","trackFile":"test3.wma"},{"id":"4","trackName":"Ain't She Sweet","trackFile":"test4.wma"},{"id":"5","trackName":"Octopus' Garden","trackFile":"test5.wma"},{"id":"6","trackName":"Teen Spirit","trackFile":"test6.wma"},{"id":"7","trackName":"Knockin on Heaven's Door","trackFile":"test7.wma"}]};
treeEl.appendChild(to_ul(treeObj.root));
treeEl.appendChild(to_ul(treeObj.root));
}
</script>
</head>
<body>
Click here
<br/>
<br/>
<br/>
<br/>
<div id="player-digital-title"></div>
</body>
</html>
To make a "link" presumably you want an anchor element inside each li element, and for the a elements you'd want to have href attributes that you don't seem to have in your data. But by way of example, assuming you want to use the id as the href you could do this:
$(document).ready(function(){
var treeObj = {"root":[{"id":"1","trackName":"Whippin Post"},{"id":"2","trackName":"Sweet Caroline"},{"id":"3","trackName":"Tears in Heaven"},{"id":"4","trackName":"Ain't She Sweet"},{"id":"5","trackName":"Octopus' Garden"},{"id":"6","trackName":"Teen Spirit"},{"id":"7","trackName":"Knockin on Heaven's Door"}]};
var $ul = $("<ul></ul>");
$.each(treeObj.root,function(i,v) {
$ul.append($("<li></li>").append(
$("<a></a>").attr("href",v.id).html(v.trackName)));
});
$("#tree").append($ul);
});
Your question was tagged with "jQuery", so I've gone ahead and created the list (with anchors inside each li) using jQuery. The $.each() "loop" iterates through each element in the treeObj.root array, creating an a element with the id and trackName, appending that to a new li element, and appending that to a ul element. After the .each() finishes the new ul is appended to your tree div.
As far as styling the links, that's up to you to do the CSS you want, but since you mention dropping the text decoration you may want to start with something like this:
#tree a { text-decoration : none; }
Working demo: http://jsfiddle.net/B2Zsv/
(If that code and output as shown in the fiddle isn't the sort of thing you're looking for I suggest you update your question to show the desired output html that you want to generate.)
UPDATE
The following variation on my original code stores the track names as attributes on the anchors created, and then retrieves them on click.
$(document).ready(function(){
var treeObj = {"root":[{"id":"1","trackName":"Whippin Post","trackFile":"test1.wma"},{"id":"2","trackName":"Sweet Caroline","trackFile":"test2.wma"},{"id":"3","trackName":"Tears in Heaven","trackFile":"test3.wma"},{"id":"4","trackName":"Ain't She Sweet","trackFile":"test4.wma"},{"id":"5","trackName":"Octopus' Garden","trackFile":"test5.wma"},{"id":"6","trackName":"Teen Spirit","trackFile":"test6.wma"},{"id":"7","trackName":"Knockin on Heaven's Door","trackFile":"test7.wma"}]};
var $ul = $("<ul></ul>");
$.each(treeObj.root,function(i,v) {
$ul.append(
$("<li></li>").append( $("<a></a>").attr({
"href":v.id,"data-file":v.trackFile}).html(v.trackName) )
);
});
$("#tree").append($ul);
$("#tree a").click(function() {
var trackname = $(this).html(),
filename = $(this).attr("data-file");
// here add your code to do something with filename and/or trackname
return false;
});
});
As you can see my click handler doesn't actually do anything with the filename once it gets it (my updated demo http://jsfiddle.net/B2Zsv/3/ displays it), but that shows you how to get the right filename so from there you can figure out how to play it...
First off create the link in js:
function to_ul(id) {
var ul = document.createElement("ul");
for (var i=0, n=id.length; i<n; i++) {
var branch = id[i];
var li = document.createElement("li");
li.innerHTML = "<a href='wherever' class='listAnchor'>" + branch.trackName + "</a>"
ul.appendChild(li);
}
return ul;
}
and then style it in css:
<style>
.listAnchor {
text-decoration: none;
}
</style>
To create an a element within the li elements, simply apply the same techniques as demonstrated in the code as you have it:
function to_ul(id) {
var ul = document.createElement("ul");
for (var i = 0, n = id.length; i < n; i++) {
var branch = id[i];
var li = document.createElement("li"),
a = document.createElement('a'); // create the `a`
a.href = "http://example.com/"; // set the `href`
var text = document.createTextNode(branch.trackName);
a.appendChild(text); // append text to the a
li.appendChild(a); // append the a to the li
ul.appendChild(li);
}
return ul;
}
JS Fiddle demo.
To style that link, you can either use CSS in your document, or in an external stylesheet (as with any other CSS):
li a:link,
li a:visited {
/* style the link's 'default' state */
}
li a:hover,
li a:active,
li a:focus {
/* style the 'interactive' states of the links */
}
JS Fiddle demo.
You could, of course, simply apply the styles directly in the JavaScript that creates said elements, though this is needlessly expensive:
/* all the other stuff removed, for brevity */
var li = document.createElement("li"),
a = document.createElement('a'); // create the `a`
a.href = "http://example.com/"; // set the `href`
a.style.color = '#000';
a.style.textDecoration = 'none';
/* ...and other stuff... */
JS Fiddle demo.
This approach, apart from being expensive, also lacks the ability to style the :hover, :active, :visited and :focus styles.