So I am trying to get this code below to work with updating a price based on an ajax onclick:
$('#main_body li[data-pricefield="special"]').delegate('onclick','change', function(e)
{
var temp = $(this).attr("id").split('_');
var element_id = temp[1];
var pricedef = $(this).data('pricedef');
if(pricedef == null)
{
pricedef = 0;
}
$("#li_" + element_id).data("pricevalue",pricedef);
calculate_total_payment();
});
Things seem to be working ok so far - when I type in the console:
jQuery('#li_273').data('pricevalue');
I do get a value of "1.00" returned which is actually being set here on the onclick command:
'onclick' => 'jQuery(\'#li_273\').data(\'pricevalue\',\'1.00\');',
My question is what is wrong with my first block of code that is stopping this from calculating the price the right way and how do I correct it?
You are using
$('#main_body li[data-pricefield="special"]').delegate('onclick','change', function(e){
// ...
});
but delegate's syntax is .delegate( selector, eventType, handler(eventObject) ) and in this case you should write
$('#main_body').delegate('li[data-pricefield="special"]','click', function(e){
// ...
});
There is no onclick event in jQuery and AFAIK an li doesn't have a change event so if you want to use multiple events then you can use
$('#main_body').delegate('li[data-pricefield="special"]','click another_event', function(e){
// ...
});
Also you can use on instead of delegate.
An Example and Read more.
'onclick', 'change' should be 'click change' in the delegate
EDIT: "$('#main_body li[data-pricefield="special"]').delegate('li','change', function(e) { actually works when you tab out of the field - it adds the $1 to it."
so you should have:
$('#main_body li[data-pricefield="special"]').on('li','click change', function(e) {
and perhaps check
if(pricedef == undefined || pricedef == null){
Related
I have a form where small labels are displayed above each field, once the user adds a value to that field.
This for is sometimes loaded with some of the fields being pre-populated.
How would i check on page load if any of the form fields have a value and if so, have the label visible?
Here's my current code for displaying labels once a field has a value:
$('.form-control').blur(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});
on page load try this:
$('.form-control').each(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});
$(document).ready(function(){
$('.form-control').each(function(){
if($(this).val() != ''){
$(this).prev().show();
}
});
});
On document ready, for each .form-control, if the input's value is not blank, do whatever code you would to show the label.
Using focusout Event wouldn't be much of an overkill, would it?
<script type="text/javascript">
(function ($) {
$(document).ready(function (e) {
// ALTHOUGH BLUR IS OK, ONE COULD SIMPLY BIND THE .form-control CLASSES TO THE focusout EVENT
// THIS ENSURES THAT THERE IS A HIGHER LIKELIHOOD THAT THE FIELD IN QUESTION ONCE HAD FOCUS
// WHICH MAY IMPLY THAT THE USER ALSO INTERACTED WITH THE FIELD IN SOME WAY...
$('.form-control').each(function(elem){
var objElem = $(this);
objElem.focusout(function(evt) {
if ($(this).val()) {
// NOW, YOU SHOULD KNOW WHICH METHOD TO USE TO TRAVERSE THE DOM
// AND GET AT THE LABEL....
// IN YOUR CASE IT SEEMS TO BE THE PREVIOUS ELEMENT BEFORE THE FORM-FIELD.
$(this).prev().show();
}
});
});
});
})(jQuery);
</script>
I have some issues with JQuery.
Code;
$(document).ready(function(){
$("#area_results").click(function(){
$("#areaclickpass2 a").click(function(){
var value = $(this).html();
var input = $('#inf_custom_TESTclubarea');
input.val(value);
$("#area_results").hide(); // hide results after click
});
});
});
The current website is requiring 2 clicks to input the value into a field.
I understand why it's doing this (Best solution I can think of to achieve the outcome), however I was curious whether it is possible to achieve the same result by only using a single click.
Thanks for your time.
Create a variable that counts up with each click and execute your code when that variable equals 2.
$(document).ready(function(){
var click_count = 0;
$("#area_results").click(function(){
click_count++;
if(click_count==2){
var value = $(this).html();
var input = $('#inf_custom_TESTclubarea');
input.val(value);
$("#area_results").hide(); // hide results after click
}
});
});
You are binding a event handler within another event handler. #areaclickpass2 will not be handled unless you click on #area_results first.
Just move $('#areaclickpass2') event binding out of #area_results scope:
$("#area_results").click(function(){
//may not even be necessary to have this
});
$("#areaclickpass2 a").click(function(){
var value = $(this).html();
var input = $('#inf_custom_TESTclubarea');
input.val(value);
$("#area_results").hide(); // hide results after click
});
i have a page for view photos and users can submit comment on it
i write a js file name(ajax.js) to load new comment with ajax every 30 sec on that pages
i write another js file (name feed.js) for when user click on delete icon near every comment comment go hide and delete
codes feed.js:
$('a[cm-del]').click(function () {
var cmid = $(this).attr('cm-del');
var typeid = $(this).attr('data-type');
$('li[last-id=' + cmid + ']').effect('pulsate');
$('li[last-id=' + cmid + ']').hide(300);
$.post('/post/comment.php', {
cmdelete: cmid,
type: typeid
}, function () {});
});
codes ajax.js:
function getmorecmphoto() {
$('li[data-hiv]').hide();
var lastid = $('li[last-id]:last').attr('last-id');
if (lastid === '') {
var lastid = 0;
}
var oldcm = $('div[comments-id]').html();
var photoid = $('div[comments-id]').attr('comments-id');
$.post('/post/photo.php', {
lastid: lastid,
photoid: photoid
}, function (getlastcmphoto) {
$('div[comments-id]').html(oldcm + getlastcmphoto);
});
}
setInterval(getmorecmphoto, 25000);
when for first time user open this page its load last 10 comment
and everythings work fine and nice
but after 25-30 sec when new comment load with ajax
my feed.js not work for new comments?
what i must to do about this?
i put feed.js load every time when new comments load buts its bad cuz in 10 minute user have about 20 feed.js loaded !
can i do something else?
thank you
You need event delegation, bind the event to the nearest static (not dynamically loaded) container and delegate the actions.
Change this:
$('a[cm-del]').click(function(){
To this:
$(document).on('click','a[cm-del]',function(){
I don't know how your markup looks like so I'm using the document as a fallback here.
Note: If you're using jQuery <1.7 use live() instead of on()
$('a[cm-del]').live( 'click', function(){
I think .live should work.
$('a[cm-del]').live("click",function(){
var cmid = $(this).attr('cm-del');
var typeid = $(this).attr('data-type');
$('li[last-id='+cmid+']').effect('pulsate');
$('li[last-id='+cmid+']').hide(300);
$.post('/post/comment.php',{cmdelete:cmid,type:typeid},function(){
});
});
If not work then do like this ..
first change the feed.js like
function SetEvent(){
$('a[cm-del]').unbind("click").click(function(){
var cmid = $(this).attr('cm-del');
var typeid = $(this).attr('data-type');
$('li[last-id='+cmid+']').effect('pulsate');
$('li[last-id='+cmid+']').hide(300);
$.post('/post/comment.php',{cmdelete:cmid,type:typeid},function(){
});
});
}
Now change ajax.js like
function getmorecmphoto(){
$('li[data-hiv]').hide();
var lastid = $('li[last-id]:last').attr('last-id');
if(lastid === ''){
var lastid = 0;
}
var oldcm = $('div[comments-id]').html();
var photoid = $('div[comments-id]').attr('comments-id');
$.post('/post/photo.php',{lastid:lastid,photoid:photoid},function(getlastcmphoto){
$('div[comments-id]').html(oldcm + getlastcmphoto);
SetEvent();
});
}
setInterval( getmorecmphoto, 25000 );
I think it will work. Thanks
I think you can fix that by changing first line of your feed.js code to this: $('a[cm-del]').live('click', function(){
When I make a selction on the calendar and add an event, it sends the info to my PHP script and the event is placed in the database as expected. Great!
The problem is when I select another date and add the event (without refresh), it adds the event twice in the MYSQL database. Not Great!
This perpetually adds up with the increasing clicks until a refresh is made. Also the "extra" events added have no time placed in the DB except for "0000-00-00 00:00:00", so in turn it appear correct upon output. Am I just missing something simple? What am I doing wrong?
<script>
$(document).ready(function(){
select: function(start, end, allDay){
$("popup").show();
$(".title").focus();
var start = Date.parse(start) / 1000;
var end = Date.parse(end) / 1000;
var allDay = allDay:
$("submitForm").click(function(){
var title = $(".title").val();
var description = $("description").val();
var team_id = <?php echo $id ?>;
if(title != "" || title != " "){
$.post("script.php", {...}, function(){
$(".title").val("");
$(".description").val("");
start = "";
end = "";
title = "";
description = "";
team_id = "";
allDay = "";
calendar.fullCalendar('unselect');
calendar.fullCalendar('refetchEvents');
});
}else{
alert("You must enter a title");
}
$(".popup").hide();
});
}
});
</script>
Obviously this is not the entire JS/Jquery but this is what matters. What am I missing? Thanks in advance. Your help is appreciated!
Here you attach the click event repeatedly when you select a date.
select: function(start, end, allDay){
$("submitForm").click(function(){});
})
So, if your $("popup") exist on document.ready just put this line of code, outside
of the definition of the plugin
Here submit form is found and click event is attached, no matter if the element has display:none property:
$(document).ready(function() {
$("submitForm").click(function(){});
$('#calendar').fullCalendar({
select:select: function(start, end, allDay){})
})
})
if you add the $("popup") element, later dynamically in the jQuery script you have to bind the click event, but be careful to put that code somewhere where it will be executed only once.
I guess your click is getting attached on each success. Place your code click function outside your success callback.
I actually had to add a random session id to fix the problem and then clear the id after the post was completed.
You need to use the following in your success function:
$('#save_button').unbind()
So that as per #mehul9595 you remove all the accumulated events bound to the click.
Help from: https://stackoverflow.com/a/9688908/2468603
I have a multi level select chain, where by the value of the first select generates the options for the next select list. And within the second list some of the values will cause a div to display with another input.
My codes (below) seem to work just fine when tested on static content (ie: the second select is hard coded in the html). But when I add it with JQuery, the second level no longer triggers the .change function.
<script type="text/javascript">
$(document).ready(function() {
$dts = $("select[name='tourdes']");
$dts.change(function() {
var dtsValue = $(this).val();
var dtsString = '?tourdes=' + dtsValue;
$('#dateSelect').show();
$('#dateSelect').load('include/avdates.php' + dtsString).append();
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$tags = $("select[name='tourcode']");
$tags.change(function() {
if ($(this).val() == "private") {
$(".prvcal").css({"visibility":"visible"});
}
});
});
</script>
I am guessing something needs to be re-initialized, but I am getting no where with my experiments.
If you're using jQuery 1.7 you'll want to use on, as both live and delegate are deprecated.
$(document).on("change", "select[name='tourcode']", function() {
var dtsValue = $(this).val();
var dtsString = '?tourdes=' + dtsValue;
$('#dateSelect').show();
$('#dateSelect').load('include/avdates.php' + dtsString).append();
});
docs for on()
You are probably using dynamically-generated HTML elements. If that is the case, you need to use .delegate() to handle them:
$('select').delegate("[name='tourdes']", 'change', function() {