i have a div name all contact in which there is many name when i click anyone name this name show me in other div name receiver its work fine problem is this when page reload name gone from reciver div which i click will you please tell me how to made it when page refresh div data not gone untill i go to other page.
#usersOnLine {
font-family:tahoma;
font-size:12px;
color:black;
border: 3px teal solid;
height: 625px;
width: 300px;
overflow-y:scroll;
}
<div id='receiver'>
<h3>receiver</h3>
<!-- NOTICE THAT THIS IS NEW NOW... -->
<!-- IT IS IMPORTANT THAT YOU HAVE THIS PARAGRAPH EXACTLY AS IT IS HERE -->
<p class='bubbled-data'></p>
</div>
<div id = 'contact'>
<h1 align="left"> all contacts </h1>
<div id="usersOnLine">
<h2>
<?php
foreach ($result as $key => $val) {
echo "<span class='clickAble'>" . $val['email'] . "</span>";
echo "<br>";
echo "<br>";
}
?>
</h2>
</div>
</div>
</div>
<!-- JAVASCRIPT - JQUERY -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
(function ($) {
$(document).ready(function (e) {
var receiver = $("#receiver");
var clickAbles = $(".clickAble");
// BIND THE clickAble SPAN TO AN ON-CLICK EVENT...
// SO THAT WITH EACH CLICK, YOU CAN SIMPLE APPEND THE CONTENT OF THAT SPAN TO THE RECEIVER DIV
clickAbles.on("click", function (evt) {
var nameValue = $(this).text();
receiver.find(".bubbled-data").text(nameValue);
$("#reciver_email").val(nameValue);
});
});
})(jQuery);
</script>
So you want retain the name in receiver div after page reload.
You can use html Local Storage,
Refer HTML Local Storage Objects
<script type="text/javascript">
(function ($) {
$(document).ready(function (e) {
var receiver = $("#receiver");
var clickAbles = $(".clickAble");
if(window.performance)
{
if(performance.navigation.type == 1 )
{
var nameValue = localStorage.getItem("nameValue");
receiver.find(".bubbled-data").text(nameValue);
$("#reciver_email").val(nameValue);
receiver.find(".bubbled-data").text(nameValue);
}
}
// BIND THE clickAble SPAN TO AN ON-CLICK EVENT...
// SO THAT WITH EACH CLICK, YOU CAN SIMPLE APPEND THE CONTENT OF THAT SPAN TO THE RECEIVER DIV
clickAbles.on("click", function (evt) {
var nameValue = $(this).text();
receiver.find(".bubbled-data").text(nameValue);
$("#reciver_email").val(nameValue);
localStorage.setItem("nameValue", nameValue);
});
});
})(jQuery);
</script>
Try this.
Related
I'm having a table listing some comments. Next to the comment there are two links, the hide and show. So what I want to do is when clicking the right link to change the comment's status with ajax.
// inside a loop for showing all comments
<div class="pull-right" class="publish-history-comment">
Publish
</div>
<div class="pull-right" class="hide-history-comment">
Hide
</div>
<?= $row->comment; ?>
<script type="text/javascript">
function toggleHistoryNotes(status) {
var link = $('.' + status + '-history-comment-link');
var time = link.attr('data-time');
alert(time);
}
</script>
How can I target which link was clicked and do the ajax call to toggle the comment status?
You can change your JQuery to trigged on click on a tag. Also, you should add e.preventDefault(); as this is gonna be click on the a tag, and this would prevent default action.
$('.pull-right a').on('click', function(e) {
e.preventDefault();
var time = $(this).data('time');
console.log($(this).text() + ' == ' + time);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pull-right" class="publish-history-comment">
Publish
</div>
<div class="pull-right" class="hide-history-comment">
Hide
</div>
<script type="text/javascript">
$('a').on('click', function(e) {
e.preventDefault();
var time = $(this).data('time');
});
</script>
I was following a tutorial on YouTube about how to display a popup box after the click of a button. It was fairly simple but now I want to twist things a little bit. I want to display the markup inside a PHP IF function.
I believe that creating a JavaScript function would be the road to follow but I am not proficient in JavaScript/jQuery as I am only starting with it now.
I want to display the following markup should my PHP IF function equate to TRUE
<div id="popup-box" class="popup-position">
<div class="popup-wrapper"> <!-- move away from screen and center popup -->
<div class="container"> <!-- backgorund of pop up -->
<h2>Pop box<h2>
<p>Close popup</p>
</div>
</div>
</div>
The following JavaScript function is used in the tutorial that I was following. It works perfectly when it is triggered by onClick.
<script>
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
I have the following PHP script
function cart($userEmailAdd){
global $dbc; // database connection variable
/*
Verify if the product that is being added already exists in the cart_product table.
Should it exist in the cart then display popup box with an appropriate
message.
Otherwise, add the product to cart_product
*/
if(isset($_GET['cart'])){
$productID = $_GET['cart'];
$queryCheckCart = "SELECT * from cart_product WHERE emailOfCustomer = '$userEmailAdd' AND cpProductid = '$productID'";
$executeCheckCart = mysqli_query($dbc, $queryCheckCart) or die (mysqli_error($dbc));
if(mysqli_num_rows($executeCheckCart) > 0 ){
/* IF MYSQLI_NUM_ROWS is greater than zero then
it means that the product already exists in the cart_product table.
Then display following markup*/
?>
<div id="popup-box" class="popup-position">
<div class="popup-wrapper"> <!-- move away from screen and center popup -->
<div class="container"> <!-- backgorund of pop up -->
<h2>Pop box<h2>
<p>X</p>
</div>
</div>
</div> <!-- -->
<?php
} else {
$query = "INSERT INTO cart..." ;
// rest of script continues after this for insertion of the product
How do I go about using the same function or a similar one without using onClick to display the markup?
you can just add inline css display:block so that the popup is displayed by default when page load.
<div id="popup-box" style="display:block" class="popup-position">
and then edit the close button of the popup to tell him to call toglle_visibility() onclick
<p>X</p>
of course you will need yo put your toggle_visibility() function in a script tag (better before the closing body element)
<script>
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
You can do something similar to this:
function cart($userEmailAdd){
global $dbc; // database connection variable
/*
Verify if the product that is being added already exists in the cart_product table.
Should it exist in the cart then display popup box with an appropriate
message.
Otherwise, add the product to cart_product
*/
if(isset($_GET['cart'])){
$productID = $_GET['cart'];
$queryCheckCart = "SELECT * from cart_product WHERE emailOfCustomer = '$userEmailAdd' AND cpProductid = '$productID'";
$executeCheckCart = mysqli_query($dbc, $queryCheckCart) or die (mysqli_error($dbc));
if(mysqli_num_rows($executeCheckCart) > 0 ){
/* IF MYSQLI_NUM_ROWS is greater than zero then
it means that the product already exists in the cart_product table.
Then display following markup*/
?>
<script>
$(document).ready(function(){
toggle_visibility('popup-box');
});
</script>
<div id="popup-box" class="popup-position">
<div class="popup-wrapper"> <!-- move away from screen and center popup -->
<div class="container"> <!-- backgorund of pop up -->
<h2>Pop box<h2>
<p>X</p>
</div>
</div>
</div> <!-- -->
<?php
} else {
$query = "INSERT INTO cart..." ;
// rest of script continues after this for insertion of the product
All you have to do is to tell javascipt that it has to open popup. Here, I made Javascript run function toggle_visibility('popup-box'); after the document loads.
The popup div doesn't have to be inside php's IF statement. And instead of using $(document).ready(function(){ }); you can use onLoad="toggle_visibility('popup-box')" attribute in <body> element.
Instead of executing the html block within the php if clause, you could use a simple boolean variable to indicate whether or not to show the popup:
$showPopup = false;
if(mysqli_num_rows($executeCheckCart) > 0 ){
/* IF MYSQLI_NUM_ROWS is greater than zero then
it means that the product already exists in the cart_product table.
Then display following markup*/
$showPopup = true;
?>
<?php
} else {
Then in your html code you could show the popup based on what the $showPopup has been set to:
<div id="popup-box" <?php echo ($showPopup === false)? 'style="display:none"' : '' ?> class="popup-position">
</div>
I have a form where I am cloning the initial input fields as a template to allow the user to add as many options as they need to. Adding new fields works fine, but when attempting to remove them, the first one works as expected but all generated fields cannot be removed.
My knowledge of JS is very poor but I would assume that setting it to remove the parent should be the correct operation.
<script type="text/javascript">
$(function()
{
var template = $('#inventoryItems .inventory:first').clone(),
inventoryCount = 1;
var addInventory = function()
{
inventoryCount++;
var inventory = template.clone().find(':input').each(function()
{
var newId = this.id.substring(0, this.id.length-1) + inventoryCount;
$(this).prev().attr('for', newId); // update label for (assume prev sib is label)
this.name = this.id = newId; // update id and name (assume the same)
}).end() // back to .attendee
.attr('id', 'inv' + inventoryCount) // update attendee id
.appendTo('#inventoryItems > fieldset'); // add to fieldset
};
$('.btnAddInventory').click(addInventory); // attach event
});
$(function()
{
var removeInventory = function()
{
$(this).parent().remove();
};
$('.btnRemoveInventory').click(removeInventory); // attach event
});
</script>
HTML:
<div id="inventoryItems" class="inventoryItems" style="margin:0; padding:0;">
<fieldset style="width:62%; float:left; margin-left: 19%;">
<label>Inventory</label>
<div id="inv1" class="inventory">
<select name="invItem1" style="width:92%;">
<?php
$invItem_values = array("id", "name");
display_options_list($dp_conn, $invItem_values, "inventory", "id");
?>
</select>
<input class="btnRemoveInventory" type="button" style="background: url(images/icn_trash.png) no-repeat; cursor:pointer; border: none;">
</div>
</fieldset><div class="clear"></div>
<!-- Add Inventory Button -->
<div style="width:62%; margin:0; padding:0; float: right; margin-right: 19%">
<input class="btnAddInventory" type="button" value="Add Item" style="float: right;">
</div><div class="clear"></div>
</div>
Attaching events via click() (or any other shortcut event handler) only works with elements which are available on load of the page. Instead, because you are appending elements dynamically, you need to use a delegated event handler. Change this:
$('.btnRemoveInventory').click(removeInventory)
To this:
$('#inventoryItems').on('click', '.btnRemoveInventory', removeInventory)
This is attaching the event to the nearest static element, #inventoryItems, and then filters the events raised to see if the target matches .btnRemoveInventory.
I have this contact us button sticking to the left of the screen which has a form attached which is hidden by default(because it has negative left of (-472px)).
Now When I click on it left becomes 0px and form slides in(using animate method) with the contact us button itself.
When I click again on the contact us button form and button slides back and hide having left property set to -472 again.
Now I want it also to hide if use click somewhere else on the screen instead of click just the button. I've tried but now its slides in and slides back and hide at the same time.
How can I make it work properly.
code where the problem is: jquery code which is not working is that the bottom of the code like
// clicking somewhere else ------------------------------------------------------------------
jQuery("body").click(function(e){
Full code
<style>
#form_contact_wrapper{position: absolute;top: 225px;left: -472px;z-index: 999;}
#contact-btn-div{top: 267px;float:left;}
#form_contact {z-index: 999;width: 450px;border: 1px solid;border-color: #BBB;padding: 10px;background-color: #FFF;float:left;}
#form_contact p.required{width: 450px;}
</style>
<?php if(Mage::helper('customer')->isLoggedIn() && Mage::app()->getStore()->getCode()=="default"):?>
<div id="form_contact_wrapper">
<div id="form_contact" class="form_contact">
<div id="form_contact_container" class="form_contact_container">
<div class="form_contact_div">
<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="webforms/form" webform_id="' . Mage::getStoreConfig('webforms/contacts/webform') . '" template="webforms/default.phtml" redirect_url="www.google.com" }}');
echo $_widget;
?>
<a id="contact_new_question_link" href="javascript:void(0)" style="display: none;text-decoration: underline;"><span><span id = "contact_ask_another">Ask another Question</span></span></a>
</div>
</div>
</div>
<div id="contact-btn-div"><img src="<?php echo $this->getSkinUrl('images/contact-us.png'); ?>"></div>
</div>
<script type="text/javascript">
jQuery(function(){
var webform_id = <?php echo Mage::getStoreConfig('webforms/contacts/webform'); ?> ;
var webform_label = jQuery("#webform_"+webform_id+"_form form div ul li div label" );
var webform_fields = jQuery("#webform_"+webform_id+"_form form div ul li div label").next().children();
webform_label.css('width', '100px');
webform_fields.css("width","250px");
// with sucess text
if(jQuery('#form_contact_container .webforms-success-text').length == 1){
jQuery("#form_contact_wrapper").animate({'left': '0px'});
jQuery("#form_contact_wrapper").addClass('active');
jQuery('#form_contact_container .webforms-success-text').show();
jQuery('#form_contact_container .webforms-success-text p').attr('id','success_msg');
jQuery("a#contact_new_question_link").show();
jQuery("a#contact_new_question_link").addClass('active');
jQuery("#webform_"+webform_id+"_form").css({display:'none'});
jQuery("#contact-btn-div").toggle(function(){
jQuery("#form_contact_wrapper").removeClass('active');
jQuery("#form_contact_wrapper").animate({'left': '-472px'});
},function(){
jQuery("#form_contact_wrapper").animate({'left': '0px'});
jQuery("#form_contact_wrapper").addClass('active');
});
if(jQuery("ul.messages").length > 0){
//
jQuery("#feedback_btn_form").after(jQuery("ul.messages"));
}
}
// without success text
if(jQuery('#form_contact_container .webforms-success-text').length == 0){
jQuery("#contact-btn-div").click(function(){
if(jQuery("#form_contact_wrapper").hasClass('active')){
jQuery("#form_contact_wrapper").animate({'left': '-472px'});
jQuery("#form_contact_wrapper").removeClass('active');
}else{
jQuery("#form_contact_wrapper").animate({'left': '0px'});
jQuery("#form_contact_wrapper").addClass('active');
}
});
}
// new question
jQuery("a#contact_new_question_link").click(function(){
// jQuery("#form_contact_wrapper").animate({'left': '-472px'});
jQuery('#form_contact_container .std.webforms-success-text').css({display:'none'});
jQuery(this).hide();
jQuery("#webform_"+webform_id+"_form").show();
});
// clicking somewhere else ------------------------------------------------------------------
jQuery("body").click(function(e){
if(jQuery("#form_contact_wrapper").hasClass('active')){
if(jQuery(e.target).closest('#form_contact').length == 0 && e.target.id != '#contact-btn-div'){
jQuery("#form_contact_wrapper").removeClass('active');
jQuery("#form_contact_wrapper").animate({'left': '-472px'});
}
}
});
});
</script>
<?php endif; ?>
Please suggest something. Thanks a lot.
Use .blur() method. The blur event is sent to an element when it loses focus. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page. Read more
Ok, I'm having a major headache with simplemodal - I know I'm almost there, but can't get this to quite work right. I have a simplemodal dialog that has several dynamically created divs inside it, such as
<html>
<head>
<!-- Confirm CSS files -->
<link type='text/css' href='css/confirm.css' rel='stylesheet' media='screen' />
</head>
<body>
<div id='container'>
<h1>Test Page</h1>
<div id='content'>
<div id='confirm-dialog'>
Page Content Goes Here
</div>
<!-- modal content -->
<div id='confirm'>
<div class='header'><span>Header Text</span></div>
<div class='message'>
<div id='optionDiv0'><input type='radio' id='options' name='options' value='0' />Option0</div>
<div id='optionDiv1'><input type='radio' id='options' name='options' value='1' />Option1</div>
<div id='optionDiv2'><input type='radio' id='options' name='options' value='2' />Option2</div>
</div>
<div class='buttons'>
<div class='yes'>OK</div>
</div>
</div>
</div>
<!-- Load JavaScript files -->
<script type='text/javascript' src='scripts/jquery.js'></script>
<script type='text/javascript' src='scripts/jquery.simplemodal.js'></script>
<script type="text/javascript">
jQuery(function ($) {
$('#confirm-dialog input.confirm, #confirm-dialog a.confirm').click(function (e) {
e.preventDefault();
confirm("", function () {
for(var k = 0; k <= 3; k++) {
if(options[k].checked) {
var ele = document.getElementById("optionDiv" + k);
ele.style.display = "none;";
//alert("Stop Here");
}
}
});
});
});
function confirm(message, callback) {
$('#confirm').modal({
closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
position: ["20%",],
overlayId: 'confirm-overlay',
containerId: 'confirm-container',
containerCss: {
height: 300,
width: 450,
backgroundColor: '#fff',
border: '3px solid #ccc'
},
onShow: function (dialog) {
var modal = this;
$('.message', dialog.data[0]).append(message);
// if the user clicks "yes"
$('.yes', dialog.data[0]).click(function () {
// call the callback
if ($.isFunction(callback)) {
callback.apply();
}
// close the dialog
modal.close(); // or $.modal.close();
});
}
});
}
</script>
</body>
</html>
In the click code, I'm trying to make it so when the user clicks on one of the radio buttons, then clicks 'OK', that item is no longer visible in the popup. If I follow that code with an alert("Stop here");(shown in the code above, commented out), then I can see the div disappear from the popup. But once I clear the alert box, (or if I comment it out so it never runs), the next time I activate the dialog, the div that I hid is re-appearing. How can I keep it hidden, so that it remains hidden the next time the dialog is activated, or is that possible? Thanks in advance.
FINAL EDIT: Found the solution for the dialog box reverting to its original state every time it opens. I pasted this in just above the jquery code, and it works like a charm:
<script> $.modal.defaults.persist = true; </script>
Found on this site in another thread, as part of a different question. Thanks for all who helped.
Your code still doesn't look complete to me, but as for your confirm function callback
confirm("", function(){
$('#confirm input[type=radio]').each(function(){
if($(this).is(':checked'))
$(this).parent().empty();
});
});
Like that?