I would like to append some html to a div when a button is clicked. The only thing that needs to be dynamic about it is that it needs to have a different id the the otherwise identical html above it. So if I have a structure like this:
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
and to the end of container i would like to append
<div id="div3" class="inner">
<p id="P3">Some content</p>
</div>
What would be a good method to store this HTML? Keep it all in a php page and post the number the new div's id should be incremented to? Or is is smarter to put the html inside a string, and have a regular expression increment all of the numbers. Or is there some incredibly obvious way to do this that I've completely missed.
Jquery is an option, as that's already on the page.
Also, if this question seems too open-ended, please let me know in the comments how I can change it before closing it.
Thank you very much.
Based on your answers to comments so far (i.e. that the id's aren't really important, and that you will have input elements in the content), I would say that you would want to redo your html to look like this:
<div id="container">
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
</div>
Note there are no longer id's and I have used array notation for your input elements such that they will be posted as an array to the receiving script (eliminating the need to increment counters in javascript or parse different posted variable names into a usable array in PHP on the server).
And then use something like this in jQuery
$('#button_id').click(function() {
$('#container > div.inner:last').clone().val('').appendTo('#container');
});
Put the HTML you want to add each time into a hidden div, then use jQuery to copy and add that to the relevant location. For instance:
<div id="appendContents" style="display:none;">
<div class="inner">
<p id="P3">Some content</p>
</div>
</div>
then jquery:
var shownDivs = <?php echo $numberOnScreenFromBeginning;?>;
$('#theButtonYouWantClickable').click(function()
{
var newContent = $('#appendContents').clone();
newContent.$('div.inner').get(0).id = 'div'+shownDivs;
newContent.$('div.inner p').get(0).id = 'P'+shownDivs;
// Where does P content come from??
shownDivs++;
$('#container').append(newContent);
});
Try this Please: Working Demo http://jsfiddle.net/eNXmr/
In the code below it will calculate the lenght of the div hence dynamically set the id as 1, 2, 3, and beyond.
Hope it fits you need :)
API used: http://api.jquery.com/append/
Code
$('#hulk').click(function() {
alert("Total div inside ==> " + $('#container').find('.inner').length);
var next_num = parseInt($('#container').find('.inner').length) + 1;
$('#container').append(' <div id="div' + next_num + '" class="inner"><p id="P' + next_num + '">Some content</p></div>');
});
HTML
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
<input type="button" value="Click hulk" id="hulk" />
Related
I split my body content in two parts. The left one has a map and buttons. When I click on the button, I get the result from Arad.php in another window.
How can I set the target to the second half (split right) of my body?
<body>
<div class="split left">
<div class="centered">
<h2>Button on Image</h2>
<p>Add a button to an image:</p>
<div class="container">
<img src="Harta_Romaniei.jpg" alt="Harta_Romaniei" style="width:100%">
<button class="btnarad"; onclick= "window.location.href='Arad.php'"; Target="split right">Arad</button>
<button class="btntimisoara">Timisoara</button>
</div>
</div>
</div>
<div class="split right">
<div class="centered">
<h2>Information</h2>
// I want the information here!
</div>
</div>
</body>
It doesn't have much to do with PHP, you should really do it with HTML + JavaScript.
The window.href.location will always redirect you to another route, and will never do what you want.
You'll need something like this: Simple Load an HTML page from javascript based on window width
Using dom I want to get content from some specific div.
My Current situation is this : I want to get content from site link whose html structure is as below. I want that css class data for my local database. I think this is possible only using dom.
Here is the code :
<div class="listing-info-right ">
<div class="list-card-field name-address">
<div class="list-card-user">
Mr Bimal Kumar Agarwal
</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-phone">
+91-9386750700,+91-612-2530310,+91-612-2530311
</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-email">enquiry#elementguestline.com</div></div>
<div class="list-card-field name-address list-card-border">
<div class="list-card-location">28,, Nalini Aparment 4th Floor, Kidwaipuri, Fraser Road, Patna, 800001, Bihar</div>
</div>
</div>
I want to fetch data of div class named : list-card-user, list-card-phone and list-card-location.
Before I had done coding using dom with css id, but could not get anything with class name..!
Please help me with some code.
Thanks in Advance.
You can select your div with document.getElementsByClassName in Javascript. This will return an array of all the elements with that class, if you only have one occurence then simply get the first node.
var div = document.getElementsByClassName("list-card-user")[0];
The div variable now points to your div, you can use a bunch of functions on it to get different things. For example, if you want its content, you can do this :
var content = div.innerHtml;
Edit I used Javascript because your original question used the Javascript tag, which has been removed now with an edit.
This is the simple one, perhaps this helps you :)
<div class="listing-info-right ">
<div class="list-card-field name-address">
<div class="list-card-user">Mr Bimal Kumar Agarwal</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-phone">+91-9386750700,+91-612-2530310,+91-612-2530311</div>
</div>
<div class="list-card-field name-address">
<div class="list-card-email">enquiry#elementguestline.com</div>
</div>
<div class="list-card-field name-address list-card-border">
<div class="list-card-location">28,, Nalini Aparment 4th Floor, Kidwaipuri, Fraser Road, Patna, 800001, Bihar</div>
</div>
</div>
Output:
<div id="myNamez"></div>
<div id="myPhonez"></div>
<div id="myLoc"></div>
Using jQuery:
<script>
$(document).ready(function () {
var namez='list-card-user';
var phonez='list-card-phone';
var locationz='list-card-location';
document.getElementById('myNamez').innerHTML='namez';
document.getElementById('myPhonez').innerHTML='phonez';
document.getElementById('myLoc').innerHTML='locationz';
});
</script>
Here's the demo
i need help to passing row from mysql database into div popup window?
how do i passing product id into popup div which i called through View?
For Example I Want Like This
i want to fetch product details into `product quick view popup window`
through product `ID`?
Here My Code
<div class="latest_products_sec">
<div class="latest_products">
<div class="title_box">
</div>
<!--Latest Products Slider -->
<div class="latest_pro_box">
<h4>Latest Shirt's Collection</h4>
<div class="latest_products_slider">
<?php
$queryshirt=mysql_query("select image,id from products
where cid='1' LIMIT 5") or die ('die shirt query');
while($rowshirt=mysql_fetch_array($queryshirt))
{
echo '<ul>';
echo '<li><img src="admin/'.$rowshirt['image'].'"
width="225" height="300" alt="" />
View</li>';
echo '</ul>';?>
}
?>
#shirt Div Popup Window
<div id="shirt" class="proquickview">
<span class="close_btn" onclick="parent.jQuery.fancybox.close();">Close</span>
<h2 class="title">quick view</h2>
<div class="quickviewinfo">
<?php
// I Need To Get ID Here
echo $id=$_REQUEST['id'];
?>
<div class="quickviewinforight">
<div class="titlerow">
<h2>Latest Shirt's Collection</h2>
<div class="start_ratings">
<div class="start_rating">
</div>
</div>
<div>
<div class="quick_review">
<h3>Quick Discription</h3>
<p>TEST.</p>
</div>
<div class="qty_row">
<div class="qty_field">
<label>Select Quantity</label>
<span>
<select name="S">
<option>5</option>
</select>
</span>
</div>
<br class="clear" />
<span class="total_price">Price <strong>$88.00</strong>
<del>$102.00</del></span>
ADD TO CART
</div>
<div class="total_price_row">
</div>
</div>
</div>
</div>
Javascript For Popup Window
$(document).ready(function() {
$(".view_btn").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
$(document).ready(function(){
// Cufon Functions //
Cufon.replace ('.latest_products_slider ul li a.view_btn',{hover:true});
});
It's hard to tell what is wrong as you have only given part of the story.
Fundamentally you need:
Action by the user (usually a click) triggers AJAX call sending a unique ID onclick="updatefunction(this.id) in a SPAN or DIV will do the trick:
<SPAN onclick="updatefunction(this.id)" id="1234">Your Stuff to update</SPAN>
The JS function updatefunction must send the detail/ID to a separate PHP script (you can use the original but this just makes it clunky) which queries your database on the ESCAPED data you send.
Once the data is ready, the JS will update the window/pop it up.
AFAICS you are missing the extra bit that actually does the querying or not applying a suitable onclick.
Why not have a look at the excellent W3Schools Ajax tutorial - it will help though does not use jQuery.
So, first, a thanks to a guy named Marcel Gwerder, he wrote the code I am asking a question about. I WOULD write a comment or PM'ed him, but I felt the thread was dead (and people don't just look at old threads right? They can't be bumped either) and I didn't know how to PM People.
Look at this code
// Shop Stuff
var cart = [];
$(document).ready(function(){
var buttonTxt = '';
$(".buyinfo").click(function() {
//Store text and id of the selected element
var txt = $(this).siblings('.shopitemname').text();
var id = $(this).closest('.shopitem').attr('id');
if(!$(this).hasClass('added')) {
buttonTxt = $('.buyinfoname', this).text();
$('#box_item').text(txt);
cart[id] = txt;
//Change text
$('.buyinfoname', this).text('Added to cart - Click to remove');
$(this).addClass('added');
//Show and hide overlay
$('#confirmbox').show('normal').delay(2000).fadeOut();
} else {
delete(cart[id]);
$(this).removeClass('added');
$('.buyinfoname', this).text(buttonTxt);
}
console.log(cart);
alert(cart);
});
});
It is some Javascript.
Now for some HTML which was recommended by a commenter.
<div id="shop">
<input type="button" value="Go To Checkout" id="checkoutbutton" />
<div class="shopitem">
<p class="shopitemname">Orange Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Black Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Green Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Blue Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Yellow Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Purple Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
</div>
</section>
<div id="confirmbox">
<p>The item was successfully added to your cart</p>
</div>
I was wondering how to send over the values in the CART variable to a new page that someone can click called "checkout.php". I was thinking AJAX from jquery or post from PHP but both are hard for me (I'm a noob coder) and I don't want to work hard on something that might not work out in the end.
Also, a question for Marcel (if he sees it) or a question for anyone who understands his code:
Why don't the values get in the cart, I did alert(cart) and it always displays empty alert box when there should be a value in cart.
Please assist me, and have a good day.
You can do this and I created a working JS fiddle example example. This is ONLY on the client side and you'd have to write the back end processing script because I'm not sure what you'd trying to do (save values in a database etc.).
Please look at the following JS fiddle:
http://jsfiddle.net/rDgUD/2/
You can use a .post command in jQuery:
$("#checkoutbutton").click(function () {
$.post("test.php", cart, function (data) {
//this is the reponse back from your PHP processing page that saved the variables in a database or however you were handling that.
alert("Data Loaded: " + data);
});
});
You can see how I handled the saving of the variables to your cart array in the JS fiddle. You add array elements with the .push functionality of jquery:
cart.push(id);
I also removed elements using the following code:
var removeItem = id; // item to remove
cart.splice($.inArray(removeItem, cart), 1);
Javascript runs on the users computer and PHP on the server.
So whilst you might want to use javascript to collect the data, I'm pretty sure you'll want to process the data with PHP, to avoid the user being able to interfere with it too easily.
"Think of your Ajax code as just another client to your server" from Security advice for jquery ajax data post?
I have the following code in .php (code is edited for easier understanding).
while($row = mysql_fetch_array($biznis)){
<div id='listItem'>
<div id='listPic'>
<img src='../../social/images/avatar/empty_avatar_full.jpg'></img>
</div>
<div id='listCont'>
<span>abcde<i>(request pending)</i></span>
</div>
<div id="listInfo">
<span id='info'>More info</span>
</div>
<div style='clear:both;'></div>
<div id='moreInfo'>
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style='clear:both;'></div>
}
So the code is nothing special, I just get all records from the table lined up.
So now, here is the problem, as you can see, I use the same ids for the same elements.
Jquery code
$(document).ready(function () {
$("#moreInfo").hide();
$("#info").click(function(){
$("#moreInfo").slideToggle();
});
});
OK so here are now the problems. Only first div (#moreinfo) is hidden, all the others are shown. And only first span (#info) is toggling the slider.
I tried with putting the counter for ids, and jquery each function, but nothing realy worked as i imagined.
Ty, Sebastian
Element IDs should be unique identifiers for that element. Use classnames for generic selectors:
<? while($row = mysql_fetch_array($biznis)): ?>
<div class="listItem">
<div class="listPic">
<img src="../../social/images/avatar/empty_avatar_full.jpg"></img>
</div>
<div class="listCont">
<span>abcde<i>(request pending)</i></span>
</div>
<div class="listInfo">
<span class="info">More info</span>
</div>
<div style="clear:both;"></div>
<div class="moreInfo">
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style="clear:both;"></div>
<? endwhile; ?>
And change your jQuery to:
$(function() {
$('.moreInfo').hide();
$('.info').click(function() {
$('.moreInfo').slideToggle();
});
});
Edit
$('.info').click(function() {
$(this).parent().siblings('.moreInfo').slideToggle();
});
use class and not ids because you're in a while loop. An id exists only once.
And update your JS.