i have done frame for drag and drop elements in jQuery. now i want to delete the element from the frame as per requirements.the deletion should from the keyboard. I am trying and can select the item, but cant delete the selected item.
my code like that..
//Select the element
jQuery(function() {
jQuery ("#frame").selectable();
});
//move to trash_icon
jQuery
HTML code...
<div id="frame">
<span id="title"></span>
<div id="tbldevs" > </div>
</div><!-- end of frame -->
i want to know is there any function in jQuery delete can be happened from keyboard.
You need to simply handle keyboard events, like so:
HTML:
<div id="frame">
<span id="title">My Title</span>
<div id="tbldevs">
<div id="item1"> This is item 1</div>
<div id="item2"> This is item 2</div>
</div>
<input type="button" id="sel-cancel" value="Cancel Select"/>
</div>
JS:
jQuery(function() {
//make the elements selectable
$("#tbldevs").selectable();
//handle the events in every element. Only applies to elements which can be handle focus
$('*').keypress(function(event) {
if ( event.which == 100 ) { //this is the keycode. 100 is the 'd' key. The delete key is difficult to bind.
$('.ui-selected').remove();
//you can add ajax calls here to remove items from the backend
}
});
});
NOTE: The following code only removes the elements from the HTML tree.
Related
I have three sliders in my webpage. I wrote an event for my second slider. but it gives this event to the first one. How can i give this event to the second one?
$('#owl-three').on('.initialized.owl.carousel translate.owl.carousel', function(e){
idx = e.item.index;
$('.owl-item.big').removeClass('big');
$('.owl-item.medium').removeClass('medium');
$('.owl-item.dir').removeClass('dir');
$('.owl-item.small').removeClass('small');
$('.owl-item').eq(idx+3).addClass('medium');
$('.owl-item').eq(idx+1).addClass('medium');
$('.owl-item').eq(idx+2).addClass('big');
$('.owl-item').eq(idx).addClass('small');
$('.owl-item').eq(idx+4).addClass('small');
$('.owl-item').eq(idx+4).addClass('dir');
});
Make your html like this
<div class="owl-carousel owl-theme" id="#owl-three">
<div class="item" data-dot="1"><h4>1</h4></div>
<div class="item" data-dot="2"><h4>2</h4></div>
<div class="item" data-dot="3"><h4>3</h4></div>
</div>
And use this script In this script you can do any thing as per desired slide by adding if condition I already add if condition for slide 2 you can write anything under that condition for slide 2
$('#owl-three').on('changed.owl.carousel', function(event) {
var currentSlide = $('div.active').find('.item').data('dot');
if(currentSlide == 2) {
}
});
I am trying to create a show/hide toggle button using jQuery. It seems to work, until I close the div that the button is within, making the two elements in different divs.
For example, the below works:
<div class="col span_1_of_3">
<button class="reveal">Show Filters</button>
<div class="filter-hide">
<p>CONTENT</p>
</div>
</div>
But this code does not:
<div class="col span_1_of_3">
<button class="reveal">Show Filters</button>
</div> <!-- THIS CLOSING DIV HAS BEEN ADDED -->
<div class="filter-hide">
<p>CONTENT</p>
</div>
JQuery is as follows:
$(".filter-hide").hide();
$("button.reveal").click(function(){
$(this).toggleClass("active").next().slideToggle();
if ($.trim($(this).text()) === 'Show Filters') {
$(this).text('Hide Filters');
} else {
$(this).text('Show Filters');
}
return false;
});
Can anyone tell me why this is and how to fix it? The reason I am doing this is because I need the button to be 1/3 of the page and then the content to show full width below. I can work around it but then come across other problems.
Thanks in advance.
You can traverse up to parent div using .closest() then use .next() to target its sibling.
$("button.reveal").click(function () {
$(this).closest('div').next().slideToggle();
$(this).toggleClass("active");
//Rest of code
});
$("button.reveal").click(function() {
$(this).closest('div').next().slideToggle();
$(this).toggleClass("active");
//Rest of code
if ($.trim($(this).text()) === 'Show Filters') {
$(this).text('Hide Filters');
} else {
$(this).text('Show Filters');
}
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col span_1_of_3">
<button class="reveal">Show Filters</button>
</div>
<!-- THIS CLOSING DIV HAS BEEN ADDED -->
<div class="filter-hide">
<p>CONTENT</p>
</div>
You're using .next() which gets the next sibling. As it's no longer a sibling, it no longer finds it.
Your best bet if the control/content are in separate locations is to pair them up. You can do this with a class or a data- attribute, eg:
<div>
<button class="reveal" data-content='filter1'>Show Filters</button>
</div>
<div class="filter-hide" data-filter='filter1'>
<p>CONTENT</p>
</div>
Then your button click code would be:
$("button.reveal").click(function() {
var filter = $(this).data("content");
$(this).toggleClass("active");
$(".filter-hide[data-filter=" + filter + "]").slideToggle();
Using the pattern, you can add/remove buttons and content and never have to change your javascript to handle them individually and, more importantly, you won't need to change your javascript if you change the HTML again, which you would have to if you used any form of traversing (eg .closest().next().find() would work if this scenario, but not if you changed the html).
Since you are using next() to get to the another button, closing the div on the 1st button no longer makes it available. So that you can target the div no matter where you put it on, use the following:
$("button.reveal").click(function(){
$(this).toggleClass("active");
$('.filter-hide').slideToggle(); //Change here.
if ($.trim($(this).text()) === 'Show Filters') {
$(this).text('Hide Filters');
} else {
$(this).text('Show Filters');
}
return false;
});
why is the following script targeting only the first element
Jquery:
$(document).ready(function () {
$(".edit_post_tab").click(function () {
var utility_id = $(this).attr("rel");
$(this).siblings("#post_utilities" + utility_id).toggle();
});
});
HTML/PHP:(the following is receieved through another page)
<div class="edit_post_tab" rel="'.$rand_id.'"></div>
<div class="post_utilities" id="post_utilities'.$rand_id.'" style="display:none;">
<div class="post_utilities-arrow"></div>
<div class="post_utilities-window">
<div id="post_utility" class="tab1-post" rel="'.$rand_id.'">Edit my post</div>
<div id="post_utility" class="tab2-post" rel="'.$rand_id.'">Delete my post</div>
</div>
</div>
MY QUESTION:
the problem i face is that when i post the data and prepend it it works fine but when two prepended elements exist the jquery selector only targets the first of the twoprepended elements.when i refresh the page the problem disappears
Is it possible to use jquery to use a callback in a div and show the full text in another div? Currently I have in the right div:
$(document).ready(function(){
setInterval(function(){
$.post("status.php?_="+ $.now(), {day : "<?php echo $day;?>"}, function(upcoming){
$(".ticker").html(upcoming);
}),
"html"
}, 45000);
});
And I need something like:
$(".view").hover(function(){
$("#left").load("a.php");
});
<div id="left">
<div class="show">
</div>
</div>
<div id="right">
<div class="ticker">
</div>
</div>
My php spits back html: EventID
What I want to do is hover over the hyper link and have the full status shown in .show
I know I'll have to do a query with the event number, but my search in doing this comes up empty. As always, any help is appreciated
$("body").on("hover", ".view", function(){
$("#left").load("a.php");
});
As you're adding .view to DOM after page load i.e dynamically so, you need delegate event handler for dynamic element using .on().
And instead of body you can use any parent selector of .view which is static-element.
I am trying to add a new div to the page when you click on NEW DIV?
How could I do this? Can someone point me in the right direction?
More example code is always helpful, but I'll try to give you some basics.
<div id="Content">
<button id="Add" />
</div>
$(function () {
$('#Add').click(function () {
$('<div>Added Div</div>').appendTo('#Content');
});
});
There are a bunch of different ways, it depends on what you want to do. A simple way is like this:
$('#id').html('<div>Hello</div>')
Which replaces the inner html of item with id 'id' with "Hello". So this:
<div id='id'>Old Html </div>
Would become:
<div id='id'><div>Hello</div></div>
And this:
$('#id').after('<div>Hello</div>')
Would transform the "Old Html" into this:
<div id='id'>Old Html</div>
<div Hello </div>
Check out www.visualjquery.com and select the Manipulation button. It gives you more than you could need to know in an easy to explore fashion.
Make sure to use the $(document).ready function in jQuery. You need this to "run" the script once your page is loaded and enable the action that is being called on this dummy button to append a new div to an existing div.
You can also use this to append anything you'd like to other elements on the page by using jQuery selectors such as .class or #id.
<div id="Content">
<button id="Add" />
</div>
$(document).ready(function() {
$('#Add').click(function () {
$('<div>Added Div</div>').appendTo('#Content');
});
});
Another useful option is to replace the contents of a tag with jQuery. You can do this using the .html() function.
<div id="Content">
<p id="changed">Hello World!</p>
<button id="change_content" />
</div>
$(document).ready(function() {
$('#change_content').click(function () {
$('#changed').html('Goodbye World');
});
});
Hope this helps!