dblClick() not working - javascript

I have a big jquery code..
Thing is, i want to create a online banner editor.
I have added two texts which are inside two divs. This divs are draggable. The divs in turn resides under two more divs which are resizable
Now i want to make a feature that when i cleck on the div containing the text, the div must get editable so that i can edit inline
But dont know why, the dblClick isnt working
Here's my code
<script type="text/javascript">
$(function(){
$.contextMenu({
selector: '.context-menu-icon, .context-menu-text',
build: function($trigger, e) {
// this callback is executed every time the menu is to be shown
// its results are destroyed every time the menu is hidden
// e is the original contextmenu event, containing e.pageX and e.pageY (amongst other data)
return {
callback: function(key, options)
{
if(key=='delete')
$(this).remove();
},
items: {
"edit": {name: "Edit", icon: "edit"},
"cut": {name: "Cut", icon: "cut"},
"copy": {name: "Copy", icon: "copy"},
"paste": {name: "Paste", icon: "paste"},
"delete": {name: "Delete", icon: "delete"},
"sep1": "---------",
"quit": {name: "Quit", icon: "quit"}
}
};
}
});
});
$(document).ready(function()
{
$.fn.jPicker.defaults.images.clientPath='color-picker-img/';
$('.color_text').jPicker();
$("#blink_icon").dblclick(function()
{
alert( "Handler for .dblclick() called." );
});
$(".edit_text").dblclick(function()
{
alert( "Handler for .dblclick() called." );
});
$("#accept_first_text").click(function()
{
var img_w = $('#target').width();
var img_h = $('#target').height();
var first_text_width = parseFloat(parseFloat(img_w)/2);
var first_left = parseFloat(parseFloat(img_w)-parseFloat(first_text_width))/2;
var first_top = $('#first_text_top').val();
var first_left = $('#first_text_left').val();
var first_color = $('#first_text_color').val();
var first_size = $('#first_text_size').val();
var first_text_desc = $('#first_text_area').val();
$('#drag_first_text').css({"left":first_left+"px",
"top":first_top+"px",
"position":"absolute",
"width":first_text_width+"px",
"z-index":"5"});
$('#first_text').css({'font-weight':'bold',
'color':'#'+first_color,
'font-size':first_size+'px'});
$('#first_text').html(first_text_desc);
$('#first_text').resizable();
});
$("#accept_second_text").click(function()
{
var img_w = $('#target').width();
var img_h = $('#target').height();
var second_text_width = parseFloat(parseFloat(img_w)/2);
var second_left = parseFloat(parseFloat(img_w)-parseFloat(second_text_width))/2;
var second_top = $('#second_text_top').val();
var second_left = $('#second_text_left').val();
var second_color = $('#second_text_color').val();
var second_size = $('#second_text_size').val();
var second_text_desc = $('#second_text_area').val();
$('#drag_second_text').css({"left":second_left+"px",
"top":second_top+"px",
"position":"absolute",
"width":second_text_width+"px",
"z-index":"5"});
$('#second_text').css({'font-weight':'bold',
'color':'#'+second_color,
'font-size':second_size+'px'});
$('#second_text').html(second_text_desc);
$('#second_text').resizable();
});
$("#get_image").click(function()
{
html2canvas([document.getElementById('main_canvas')],
{
onrendered: function (canvas) {
/*document.getElementById('canvas').appendChild(canvas);*/
var Imagedata = canvas.toDataURL('image/png');
var imagename = $('#image_name').val();
// AJAX call to send `data` to a PHP file that creates an image from the dataURI string and saves it to a directory on the server
$.post("save.php",{'image_link':Imagedata,'image_name':imagename},
function(img_data)
{
var final_href = $('<a/>',{'id':'final_href'});
$(final_href).prop({'href':'download.php?download='+img_data,'target':'_blank'});
var final_img = $('<img/>', {'id':'final','src':img_data});
$(final_img).css({"width":"100px","height":"auto"});
$(final_href).append(final_img);
$('#final_image').html(final_href);
ShowModalPopup('div_show_banner');
}
);
}
});
});
$("#get_animated_image").click(function()
{
html2canvas([document.getElementById('main_canvas')],
{
onrendered: function (canvas) {
/*document.getElementById('canvas').appendChild(canvas);*/
var shots = [];
var grabLimit = 400; // Number of screenshots to take
var grabRate = 50; // Miliseconds. 500 = half a second
var count = 0;
function showResults()
{
//console.log(shots);
for (var i=0; i<shots.length; i++) {
document.write('<img src="' + shots[i] + '"/>\n');
}
}
var grabber = setInterval(
function()
{
count++;
if (count>grabLimit)
{
clearInterval(grabber);
showResults();
}
var img = canvas.toDataURL("image/png");
shots.push(img);
}, grabRate);
var Imagedata = canvas.toDataURL('image/png');
var imagename = $('#animated_image_name').val();
}
});
});
$("#blink_icon").click(function()
{
var interval = 100;
var time = 90000000;
var timer = window.setInterval(function()
{
$("#drag_wrapper").css("opacity", "0.1");
window.setTimeout(function()
{
$("#drag_wrapper").css("opacity", "1");
}, 100);
}, interval);
window.setTimeout(function(){clearInterval(timer);}, time);
});
$("#upload_icon_Form").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload_icon.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$("#targeticonLayer").html(data);
},
error: function()
{
}
});
}));
$("#upload_template_Form").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload_template.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$("#targetLayer").html(data);
},
error: function()
{
}
});
}));
});
$(document).ready(function()
{
});
function dump_template(src,close_div)
{
var item_template = $('<img/>', {'id':'target','src':src});
var first_text = $('<div/>', {'id':'first_text','class':'edit_text'});
$(first_text).css({"left":"0px",
"top":"0px",
"color":"#fff",
"position":"relative",
"width":"auto",
"text-align":"center"});
$(first_text).html('Demo First Text');
var second_text = $('<div/>', {'id':'second_text','class':'edit_text'});
$(second_text).css({"left":"0px",
"top":"0px",
"color":"#fff",
"position":"relative",
"width":"auto",
"text-align":"center"});
$(second_text).html('Demo Second Text');
$('#main_canvas').html(item_template);
var width = $("#target").width();
var height = $("#target").height();;
$('#main_canvas').css({'height':height+'px','width':width+'px'});
var drag_first_text = $('<div/>', {'id':'drag_first_text','class':'context-menu-text'});
var drag_second_text = $('<div/>', {'id':'drag_second_text','class':'context-menu-text'});
$(drag_first_text).css({"left":"20px",
"top":"100px",
"position":"absolute",
"width":"auto",
"z-index":"5",
"text-align":"center",
});
$(drag_second_text).css({"left":"20px",
"top":"150px",
"position":"absolute",
"width":"auto",
"z-index":"5",
"text-align":"center"});
$(drag_first_text).append(first_text);
$(drag_second_text).append(second_text);
$('#main_canvas').append(drag_first_text).append(drag_second_text);
$(drag_first_text).draggable({grid:[1,1]});
$(drag_second_text).draggable({grid:[1,1]});
HideModalPopup(close_div);
}
function dump_icon(src,close_div)
{
var dragg_wrapper = $('<div/>', {'id':'drag_wrapper','class':'context-menu-icon'});
$(dragg_wrapper).css({"left":"20px",
"top":"20px",
"position":"absolute",
"width":"50px",
"z-index":"5"});
var item_img = $('<img/>', {'id':'icon_img','src':src});
$(item_img).css({"left":"0px",
"top":"0px"});
$(dragg_wrapper).append(item_img);
$('#main_canvas').append(dragg_wrapper);
$(dragg_wrapper).draggable({grid:[1,1]});
$("#icon_img").resizable();
HideModalPopup(close_div);
}
function choose_template()
{
$.post("showtemplate.php",{'show':'show'},
function(data)
{
$('#div_template_list').html(data);
ShowModalPopup('div_show_template');
});
}
function choose_icon()
{
$.post("showicon.php",{'show':'show'},
function(data)
{
$('#div_icon_list').html(data);
ShowModalPopup('div_show_icon');
});
}
</script>
EDIT
As someone requested, here's the html
<div class="container">
<div class="row">
<div class="span11">
<div class="demo-box">
<div class="page-header">
<h1>Dynamica Banner Creator</h1>
</div>
<h4>Upload Banner Template</h4>
<div style="margin-bottom: 10px; height: auto; overflow:hidden;">
<div class="button_div">
<a href="javascript:void(0);"
onclick="ShowModalPopup('div_upload_template');">
Upload Template
</a>
</div>
<div class="button_div">
<a href="javascript:void(0);"
onclick="choose_template()">Choose Template</a>
</div>
<div class="button_div">
<a href="javascript:void(0);"
onclick="ShowModalPopup('div_upload_icon');">Upload Icon</a>
</div>
<div class="button_div">
<a href="javascript:void(0);"
onclick="choose_icon()">Choose Icon</a>
</div>
</div>
<div style="position:relative;" id="main_canvas">
</div>
</div>
<div id="script_frame">
<h1>Enter your text details details</h1>
<div style="position:relative; height:auto; overflow:hidden;">
<div id="first_text_setter">
<div class="object_container">
<div class="span_container">
<span class="input_span">First Text Top-Pos.</span>
</div>
<div class="text_container">
<input id="first_text_top" name="first_text_top"
class="input_text"/>
</div>
</div>
<br/>
<div class="object_container">
<div class="span_container">
<span class="input_span">First Text Left-Pos.</span>
</div>
<div class="text_container">
<input id="first_text_left" name="first_text_left"
class="input_text"/>
</div>
</div>
<br/>
<div class="object_container">
<div class="span_container">
<span class="input_span">First Text Color.</span>
</div>
<div class="text_container">
<input id="first_text_color" name="first_text_color"
class="color_text"/>
<span class="jPicker">
</span>
</div>
</div>
<br/>
<div class="object_container">
<div class="span_container">
<span class="input_span">First Text Size.</span>
</div>
<div class="text_container">
<select id="first_text_size" name="first_text_size"
class="input_text">
<?php
for($i=4;$i<=32;$i=$i+2)
{?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}?>
</select>
</div>
</div>
<br/>
<div class="object_container">
<span class="input_span">First Text Des</span>
<br/>
<textarea id="first_text_area" name="first_text_area" class="input_text_area"></textarea>
</div>
<button id="accept_first_text">Get</button>
<div class="clearfix"></div>
</div>
<div id="second_text_setter">
<div class="object_container">
<div class="span_container">
<span class="input_span">Second Txt Top-Pos</span>
</div>
<div class="text_container">
<input id="second_text_top" name="second_text_top"
class="input_text"/>
</div>
</div>
<div class="object_container">
<div class="span_container">
<span class="input_span">Second Txt Left-Pos</span>
</div>
<div class="text_container">
<input id="second_text_left" name="second_text_left"
class="input_text"/>
</div>
</div>
<div class="object_container">
<div class="span_container">
<span class="input_span">Second Text Color</span>
</div>
<div class="text_container">
<input id="second_text_color" name="second_text_color"
class="color_text"/>
<span class="jPicker">
</span>
</div>
</div>
<div class="object_container">
<div class="span_container">
<span class="input_span">Second Text Size</span>
</div>
<div class="text_container">
<select id="second_text_size" name="second_text_size"
class="input_text">
<?php
for($i=4;$i<=32;$i=$i+2)
{?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}?>
</select>
</div>
</div>
<div class="object_container">
<span class="input_span">Second Txt Des</span>
<br/>
<textarea id="second_text_area" name="second_text_area" class="input_text_area"></textarea>
</div>
<button id="accept_second_text">Get</button>
<div class="clearfix"></div>
</div>
</div>
<br/>
<div style="clear:both; float:left; margin-top:30px; position:relative;">
<div class="object_container">
<span class="input_span">Image Name</span>
<input id="image_name" name="image_name"
class="input_text"/>
</div>
<br/>
<button id="get_image">Save Image</button>
</div>
<div style="clear:both; float:left; margin-top:30px; position:relative;">
<div class="object_container">
<span class="input_span">Animated Gif Name</span>
<input id="animated_image_name" name="image_name"
class="input_text"/>
</div>
<br/>
<button id="get_animated_image">Save Gif Image</button>
<button id="blink_icon">Blink</button>
</div>
</div>
</div>
</div>
</div>
</div>

There is far too much code to wade through, but the most likely cause is dynamic creation of the divs.
Use delegated event handlers instead, attached to a non-changing ancestor (document is the default if nothing closer is available). Try this:
$(document).on('dblclick', "#blink_icon", function()
{
alert( "Handler for .dblclick() called." );
});
$(document).on('dblclick', ".edit_text", function()
{
alert( "Handler for .dblclick() called." );
});
They work by listening for the specified event (dblclick in this case) bubbling up to the ancestor, then it applies the jQuery selector, then it calls the function on any matching elements that caused the event. This means they can work on elements that do not exist yet.

Related

Ajax status update in one page with one button

I am trying to update the status for my orders on the same page where it's displayed with an ajax HTML.
Displaying works just fine, but I want to set the status the the next one with only one click so I figured to use ajax for it too.
My ajax PUT for the next status
$(function () {
$(document).on('click', 'button#order_update', function (e) {
e.preventDefault();
let newStatus = '';
if ($(this).data('status') == 'pending') {
newStatus = 'confirm';
} else if ($(this).data('status') == 'confirm') {
newStatus = 'processing';
} else if ($(this).data('status') == 'processing') {
newStatus = 'picked';
}
let formStatusData = new FormData();
formStatusData.append('order_id', $(this).data('order'));
$.ajax({
type: 'PUT',
url: '{{ route("update-order-status") }}',
data: formStatusData,
success: (response) => {
console.log(response);
$(this).data('status', newStatus);
$(this).text(newStatus.charAt(0).toUpperCase() + ' order');
}
});
});
});
My ajax for the html
$.ajax({
type: 'GET',
url: '/order/view/all',
dataType: 'json',
cache: false,
success:function(response){
$('#pimage').attr('url','/'+response.product.product_thambnail);
var product_name = $('#pname').text();
var id = $('#product_id').val();
var quantity = $('#qty').val();
var OrderView = ""
$.each(response.orders, function (key,value){
var productsList = '';
$.each(value.product, function (key,value) {
productsList += `
<div class="row gx-4">
<div class="col-lg-3">
<div class="pos-task-product">
<div class="pos-task-product-img">
<div class="cover" style="background-image: url(${value.product_thambnail});"></div>
</div>
<div class="pos-task-product-info">
<div class="flex-1">
<div class="d-flex mb-2">
<div class="h5 mb-0 flex-1">${value.product_name_en}</div>
<div class="h5 mb-0">${value.pivot.qty} DB</div>
</div>
</div>
</div>
<div class="pos-task-product-action">
Complete
Cancel
</div>
</div>
</div>
</div>
`;
});
OrderView += `<div class="pos-task">
<div class="pos-task-info">
<div class="h3 mb-1" id=""><td>Üzenet: ${value.notes}</td></div>
<div><div><button type="button" class="btn btn-outline-theme rounded-0 w-150px data-status="${value.status}" data-order="${value.status}" id="order_update">Confirm Order</button></div></div>
<br>
<!-- You can safely remove this if not needed
<div class="mb-3">${value.product_id}</div>
<div class="h4 mb-8">${value.product_name}</div>
-->
<td> </td>
<div class="mb-2">
<span class="badge bg-success text-black fs-14px">${value.status}</span>
</div>
<div><span class="text">${value.created_at}</span> Beérkezett</div>
</div>
<div class="pos-task-body">
<div class="fs-16px mb-3">
Completed: (1/4)
</div>
${productsList}
</div>
</div>`
});
$('#OrderView').html(OrderView);
}
})
}
OrderView();```
**Im currently trying to use this button inside the HTML ajax**<div><button type="button" class="btn btn-outline-theme rounded-0 w-150px data-status="${value.status}" data-order="${value.status}" id="order_update">Confirm Order</button></div>
I tried using processData: false, but it just kills the process and the button is unusable. Please help.
Your problem is that you have many identifiers # with the same name.
id must be unique.
Replace in code
$(document).on('click', 'button#order_update'
to
$(document).on('click', 'button.order_update'
and
<button type="button" class="btn btn-outline-theme rounded-0 w-150px data-status="${value.status}" data-order="${value.status}" id="order_update">Confirm Order</button>
to
<button type="button" class="btn btn-outline-theme rounded-0 w-150px order_update" data-status="${value.status}" data-order="${value.status}">Confirm Order</button>
You still have the problem that you didn't close the class quote after w-150px, I closed it in the formatted code

JavaScript ajax didn't working.Where am I going wrong?

<div class="media-bottom">
#Html.TextAreaFor(model => model.Message, new { #class = "ui form", #rows = "5", #maxlenght = "300", #placeholder = "Paylaşmak istedikleriniz" })
</div>
<br />
<div class="footer-logo">
<button class=" mini ui right labeled right floated icon button" id="Button_Click" onclick="javascript: Button_Click();">
<i class="right arrow icon"></i>
Paylas
</button>
<div class="container bootstrap snippets bootdey downlines">
<div class="row">
<div class="col-md-6 col-xs-12">
<section class="widget">
<div class="widget-body">
<div class="widget-top-overflow windget-padding-md clearfix bg-info text-white">
</div>
<div class="post-user mt-n-lg">
<span class="thumb-lg pull-left mr media-object">
<img class=" img-circle" src="https://bootdey.com/img/Content/user_3.jpg" alt="...">
</span>
<span class="thumb-lg pull-right mr star">
<img src="~/Content/img/star.png" />
</span>
<div class="Thanksicon">
</div>
<div class="namespace">
<h5 class="mt-sm fw-normal text-black txt post">
#Html.DisplayFor(model => model.FullName)
<br />
<small class="text-black text-light departmen post">#Html.DisplayFor(model => model.Departmen)</small>
</h5>
</div>
</div>
<br />
<br />
<div class="text-light fs-mini m" id="Label1">
<div id="label1">
<p class="article">
#Html.DisplayTextFor(model=>model.Message)
</p>
</div>
<div class="thanksnames">
<span class="thumb-xs avatar mr-sm">
<img class="img-circle thank" src="https://bootdey.com/img/Content/user_2.jpg" alt="...">
</span>
<div class="mt-sm fw-normal text-black " id="person"> <small class="text-black text-light">Rose Tyler</small></div>
</div>
<br />
<div class="img"></div>
<div class="fs-mini text-muted text-right"><time class="time-table-top"></time></div>
</div>
</div>
</section>
</div>
</div>
</div>
This is my javascript
$(function () {
$('#Button_Click').on('click', function () {
$.ajax({
type: "POST",
url: '#Url.Action("Share")',
data: {
fullname: $("#username").val(),
departmen: $("#departmen").val(),
textarea: $(".ui.form").val()
},
datatype: "json",
success: function (data) {
$('.downlines').html(result);
}, error: function (data) {
}
});
});
});
This is my controller httppost
[HttpPost]
private JsonResult Share(SocialMedia data,string id)
{
var employee = _employeeRepository.GetById(id);
ViewBag.IsOwner = id == Session.GetEmployeeNo();
if (Session["MediaList"] == null)
{
Session["MediaList"] = new List<SocialMedia>();
}
var fullname = data.FullName;
var departmen = data.Departmen;
var textarea = data.Message;
foreach (MediaList list in data.MediaLists)
{
list.FullName = fullname;
list.Departmen = departmen;
list.Message = textarea;
list.Date = DateTime.Now.ToLocalTime();
if(data.Photo!=null)
{
list.Photo = data.Photo;
string fileName = Path.GetFileNameWithoutExtension(list.Photo);
list.Photo = "~/Image/" + fileName;
string _path = Path.Combine(Server.MapPath("~/Image/"),fileName);
}
}
return Json(new { data = PartialView("~/Views/SocialMedia/DisplayTemplates/MediaList.cshtml") });
// return PartialView("~/Views/SocialMedia/DisplayTemplates/MediaList.cshtml",
//return Json(new { success = true, message = GlobalViewRes.CreatedSuccessfully }, JsonRequestBehavior.AllowGet);
}
When I write an article and press the button, I want the page to appear below without refreshing and I want it to be repeated. It should be with the whole design. But when I press the button, I made ajax, but my ajax does not work, where am I going wrong?
Sorry for my English:)
success: function (data) {
$('.downlines').html(result);
}, error: function (data) {
}
You handle the data named 'data'. Then you are trying use it like 'result'. What is result, where is it came from? Whatever, try this
$('.downlines').html(data)

How to toggle add/remove when adding html into a textarea?

When I click on the button ADD, it should be adding all the html into the textarea, and works fine. However, If I click again I should be removing that from the text area.
An idea maybe is by pushing the inserted html into an array?
$('body').on('click', '.btn_video', function() {
var $imageSelected = $(this).parent().parent().parent().prop('outerHTML');
$('#usp-custom-5').val(function(_, currentValue) {
return currentValue + $imageSelected
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="thumbnail">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/3fJjajItnEQ?rel=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe></div><div class="caption">
<p>Duration: <span class="video-time">31:54</span></p>
<button type="button" class="btn btn-danger btn-block btn_video"><strong>ADD</strong></button>
</div>
</div>
</div>
<div class="col-xs-12 col-md-6">
<div class="thumbnail">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/3fJjajItnEQ?rel=0&modestbranding=1" frameborder="0" allowfullscreen=""></iframe></div><div class="caption">
<p>Duration: <span class="video-time">31:54</span></p>
<button type="button" class="btn btn-danger btn-block btn_video"><strong>ADD</strong></button>
</div>
</div>
</div>
</div>
</div>
<textarea name="usp-custom-5" id="usp-custom-5" data-required="false" placeholder="Example Input 5" data-richtext="false" class="usp-input usp-textarea usp-form-16" rows="5" cols="30" style="margin: 0px; height: 266px; width: 996px;"></textarea>
Here it is a Fiddle.
$('body').on('click', '.btn_video', function() {
var $imageSelected = $(this).parent().parent().parent().prop('outerHTML');
$('#usp-custom-5').val(function(_, currentValue) {
return currentValue + $imageSelected
});
});
Updated fiddle.
You could use a helper class selected for this and add/remove it by click :
$('body').on('click', '.btn_video', function() {
var content = "";
if($(this).text()=="ADD")
{
$(this).text("REMOVE");
$(this).parents(".thumbnail").parent().addClass('selected');
}else{
$(this).text("ADD");
$(this).parents(".thumbnail").parent().removeClass('selected');
}
$('.selected').each(function(){
content += $(this).prop('outerHTML');
});
$('#usp-custom-5').val(content);
});
Hope this helps.
See if it's what you need:
return currentValue + $imageSelected
replace:
return $imageSelected
If you need to save the information before erasing, paste in an array:
var arr = [];
$('body').on('click', '.btn_video', function() {
var $imageSelected = $(this).parent().parent().parent().prop('outerHTML');
$('#usp-custom-5').val(function(_, currentValue) {
arr.push($imageSelected);
return $imageSelected
});
});

How to pass content to the next tab by jQuery?

I'm a beginner at jQuery. I would like to create selectable portlet, and when it was clicked then showed the same portlet in the second tab immediately.
This is my JavaScript Code.
$(function () {
$(".frame").sortable({
connectWith: ".frame",
handle: ".portlet-header",
placeholder: "portlet-placeholder ui-corner-all"
});
$(".portlet")
.addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
.find(".portlet-header")
.addClass("ui-widget-header ui-corner-all")
.prepend("<span class='ui-icon ui-icon-minusthick portlet-toggle'></span>");
$(".portlet-toggle").click(function () {
var icon = $(this);
icon.toggleClass("ui-icon-minusthick ui-icon-plusthick");
icon.closest(".portlet").find(".portlet-content").toggle();
});
});
$(function () {
$(".frame").selectable({
stop: function () {
var result = $("#select-result").empty();
var add = $("#newExam").empty();
var count = 0;
$(".ui-selected", this).each(function () {
if (this.id > 0) {
result.append(this.id + " ");
count = count + 1;
}
});
add.append(count);
}
});
});
And this is my HTML code.
<div id="tabs">
<ul>
<li>Master</li>
<li>Add List <span id="newExam" class="badge" >0</span> </li>
</ul>
<div id="master" class="master">
<p>exam in the collection</p>
<br /><br />
<p id="feedback">
<span>You've selected:</span> <span id="select-result">none</span>.
</p>
<div class="frame" style="align-content: center">
<div class="portlet portlet-count" id="1">
<div class="portlet-header">First</div>
<div class="portlet-content">test</div>
</div>
<div class="portlet portlet-count" id="2">
<div class="portlet-header">Second</div>
<div class="portlet-content">test</div>
</div>
<div class="portlet portlet-count" id="3">
<div class="portlet-header">Third</div>
<div class="portlet-content">test</div>
</div>
</div>
</div>
<div id="creation">
</div>
P.S. Master is the first tab, Creation is the second tab.

Click handlers not firing on appended items

So I have some results that have items that get toggled, hidden/shown, etc. That bit works just fine except for the items that are appended to the bottom of the results. The click handler does not fire on them but work just fine on the others. I assume it has to do with reading the nodes at the time of page load. How do I get the appended items to also work?
<div class="event">
<a href="/profile/00000000">
<img class="user-image" src="https://graph.facebook.com/00000000/picture?width=100&height=100">
</a>
<div class="event-info">
<div class="content">
<div class="event-time-location">
<span class="user-name">Levi Thornton</span>
<span class="user-action-time">Posted 21 minutes ago</span>
</div>
<div class="event-caption">1
</div> </div>
<div class="event-like-comment">
<input id="js-eventId" type="hidden" value="201" name="eventId">
likedlike comment
more
</div>
</div>
<div class="comments" id="comments-201" style="display: block;">
<div class="newComments">
<input id="js-eventId" type="hidden" value="201" name="eventId">
<textarea class="addComment" value="Write a comment..." placeholder="Write a comment..." title="Write a comment..."></textarea>
</div>
</div>
<!-- comments -->
<div class="clear"></div>
</div>
The jQ:
...
// add like
$(".event-like").click(function(event){
event.preventDefault();
var likeBtn = $(this);
$.post('/shindig/ajax-like-event', { eventId: $(this).siblings('#js-eventId').val(), userLogged: $('#js-userLogged').val(), ajax: true}, function(data){
if(data['success']){
likeBtn.hide();
likeBtn.siblings(".event-liked").show();
} else {
if(data['noaccess']){
window.location.href = data['url'];
}
}
},"json");
});
// soft delete like
$(".event-liked").click(function(event){
event.preventDefault();
var likeBtn = $(this);
$.post('/shindig/ajax-unlike-event', { eventId: $(this).siblings('#js-eventId').val(), userLogged: $('#js-userLogged').val(), ajax: true}, function(data){
if(data['success']){
likeBtn.hide();
likeBtn.siblings(".event-like").show();
} else {
if(data['noaccess']){
window.location.href = data['url'];
}
}
},"json");
});
// hit bottom scrolling, load more results
$(window).scroll(function() {
if($(window).scrollTop() == $(document).height() - $(window).height()) {
console.log('bottom');
$.post('/index/ajax-feed-items-by-time', { pager: $("#js-pager").val(), ajax: true}, function(data){
$( ".feedContent" ).append( data );
$pager = $("#js-pager").val()+10;
$("#js-pager").val($pager);
});
}
});
Replace
$(".event-like").click(function(event) {
with
$(document).on("click", ".event-like", function(event) {
and similarly throughout your code. It's called event delegation, and the best place to start reading about it is the jQuery documentation.

Categories