javascript - How to call correctly this function in Ajax? -
i have code:
function changeeventstatusclass(object){ if ($(object).hasclass("fa-circle")) { $(object).removeclass("fa-circle").addclass("fa-circle-o"); //alert("adevarat"); } else { $(object).removeclass("fa-circle-o").addclass("fa-circle"); } } function changeeventstatus(eventid, status) { console.log(eventid, status); $.ajax({ type:'post', data: { eventid: eventid, status: status, }, url: "<?php echo $this->serverurl().str_replace('public','',$this->basepath()).'/user/ajaxupdateappointmentstatus'; ?>", success: function (result) { if (result.success) { console.log(eventid); changeeventstatusclass(eventid); //here problem } } }); } $(".crad").on('click', function(){ var eventid = $(this).attr('data-id'); var completed = ($( ).hasclass("fa-circle-o")) ? true : false; console.log(completed); if (completed) { changeeventstatus(eventid, 7); } else { changeeventstatus(eventid, 0); } })
<i class="fa fa-circle-o circle crad" data-id="241"></i> <i class="fa fa-circle-o circle crad" data-id="242"></i> <i class="fa fa-circle-o circle crad" data-id="243"></i> <i class="fa fa-circle-o circle crad" data-id="244"></i>
i have problem when try call function changeeventstatusclass
because not parameter id (it's data-id
);
how order correctly make function? site changing class buttons , want current one
can me solve problem?
thanks in advance!
you can use attribute equals selector [name=”value”], additionally can use .toggleclass()
add or remove classes.
function changeeventstatusclass(object){ $("[data-id='"+object+"']").toggleclass("fa-circle-o fa-circle"); }
however better approach pass object like
function changeeventstatusclass(element){ element.toggleclass("fa-circle fa-circle-o"); } function changeeventstatus(element, status) { $.ajax({ type:'post', data: { eventid: element.attr('data-id'), status: status, }, url: "<?php echo $this->serverurl().str_replace('public','',$this->basepath()).'/user/ajaxupdateappointmentstatus'; ?>", success: function (result) { if (result.success) { console.log(eventid); changeeventstatusclass(element); //here problem } } }); } $(".crad").on('click', function(){ var element = $(this); var eventid = element.attr('data-id'); var completed = element.hasclass("fa-circle-o"); if (completed) { changeeventstatus(element, 7); } else { changeeventstatus(element, 0); } })
Comments
Post a Comment