I have the following code, its supposed to reorder my divs within #list. And it does that just fine. Now what I want to add is: if a div does not have the attribute "data-l-disc" don't add it/hide it.
<select id="selectid">
<option id="valid1" value="def">Default order</option>
<option id="valid2" value="hl">h-l</option>
</select>
<div id="list">
---------the follow div is used about 50*-------------
<div class="dbl" data-l-disc="1">
---------blabla (like 10 other divs are in here)----------
</div>
</div>
jQuery(document).ready(function( $ ) {
$('select').on('change', function() {
if(document.getElementById('selectid').value == "hl") {
var dList = $(".dbl");
dList.sort(function(a, b){ return $(b).data("l-disc")-$(a).data("l-disc")});
$("#list").html(dList);
}
})
});
you can hide it by using
$(".dbl").not("[data-l-disc]").hide()
Related
I am trying to create a div on the bottom a of an image that when clicked, shows and when clicked again, hides. I can get it working with just one of the divs, but I am trying to do this for all of them containing the same ID.
I was first trying to get the div to initially hide and can't. Thanks for any help.
$(document).ready(function() {
var $dropdown= $('#project-description');
$dropdown.hide()
$('#project-dropdown').each(function(){
$('#project-dropdown').click(function() {
if ($menu.is(':visible')) {
$menu.slideUp("medium", function() {
$menu.hide();
});
}
else {
$menu.slideDown("medium");
}
});
});
});
UPDATE: I am closer to what I am looking for. It kind of works now that I am using classes, but all of the div's open and close together. Is there any way to get them independent of one another?
jQuery
$(document).ready(function () {
var $dropdown = $('.description-dropdown');
var $container = $('.description-container');
$dropdown.hide();
// $($menu).each(function () {
$($container, $dropdown).click(function () {
if ($dropdown.is(':visible')) {
$dropdown.slideUp("medium", function () {
$dropdown.hide();
});
} else {
$dropdown.slideDown("medium");
}
});
// });
});
Div's being manipulated
<div class="container description-container">
<div class="ui divider center aligned"><i class="chevron down icon"></i></div>
<div class="description description-dropdown">
<p><%- project.description %></p>
</div>
</div>
To make them slide independently you need to have your event code only operate on the element that was clicked. To do that you need to find the dropdown inside the container that was clicked:
$($container).click(function () {
var dd = $(this).find(".description-dropdown");
if (dd.is(':visible')) {
dd.slideUp("medium");
} else {
dd.slideDown("medium");
}
});
Note that the call to .hide inside .slideUp is not required as slideUp performs a hide.
The issue is that you're trying to use ID's. ID's can only reference to one element and a class should be used in place of that.
Using classes you can set the same class to mutliple elements and then they'll be called when needed.
Also when it comes to classes, you use a dot instead of a hash.
Hope this helps you out
$(document).ready(function()
{
var $dropdown = $('.project-description');
var $menu = $('.menu');
//$dropdown.hide();
$($dropdown).each(function()
{
$($dropdown).click(function()
{
if ($menu.is(':visible'))
{
$menu.slideUp("medium", function()
{
$menu.hide();
});
}
else
{
$menu.slideDown("medium");
}
});
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div class="project-description">
INFORMATION BLAH BLAH
</div>
<br>
<select class="menu">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<select class="menu">
<option value="cake">cake</option>
<option value="pizza">pizza</option>
<option value="garlic bread">garlic bread</option>
<option value="mac n cheese">mac n cheese</option>
</select>
<select class="menu">
<option value="games">games</option>
<option value="movies">movies</option>
<option value="music">music</option>
</select>
</body>
</html>
Rather than the "Select" type of dropdown I want a ul. But, after converting to ul, my javascript isn't firing and I can't figure out why. It works great as a Select...am I missing something?
Here is the original Select (working)
<div class="container">
<select id="selector1" name="divshowhide">
<option selected disabled value="null">Sort By:</option>
<option value="#Online-holder">Online Now</option>
<option value="#Lowest-holder">Lowest Price First</option>
<option value="#Best-holder">Best Rating First</option>
</select>
</div>
Here is the ul version: (not working)
<li>Sort
<ul class="dropdown" id="selector1">
<li>Online Now</li>
<li>Lowest Price First</li>
<li>Alpha</li>
</ul>
</li>
Here is the javascript:
<script type="text/javascript">
$(document).ready(function() {
$('#Online-holder').show();
$('#Online-link').click(function() {
$('#Online').toggle(400);
return false;
});
$('#Lowest-holder').hide();
$('#Lowest-link').click(function() {
$('#Lowest').toggle(400);
return false;
});
$('#Alpha-holder').hide();
$('#Alpha-link').click(function() {
$('#Alpha').toggle(400);
return false;
});
});
jQuery("#selector1").change(function(){
var div = jQuery(this).val();
$('.holder').hide(200);
jQuery(div).show(200);
});
</script>
Help! Thank you!
Since your element became a clickable one rather than a selectable one, you can change your change function to a click function like this:
jQuery("#selector1 li").click(function(){
var div = jQuery("a", this).attr("value");
$('.holder').hide(200);
jQuery(div).show(200);
});
I have the following dropdown:
<select name="about_performance_lead_singer" id="about_performance_lead_singer" value="">
<option value="Toni">Toni</option>
<option value="Jack">Jack</option>
<option value="James">James</option>
</select>
Below this I have the following divs:
<div class="singer_profile_overview" id="toni">...</div>
<div class="singer_profile_overview" id="jack">...</div>
<div class="singer_profile_overview" id="james">...</div>
These are set to display:none using css
I'm trying to make it so when I select the name from the dropdown, the class "visible" is added to the corresponding div - and then removed again if the selection is changed again.
Here is what I have tried so far, but with no luck:
jQuery(document).ready(function($){
$("#about_performance_lead_singer").change(function () {
$('#'(this).val()).addClass('visible');
});
});
Here is how I would do this:
html:
<select name="about_performance_lead_singer" id="about_performance_lead_singer" value="">
<option value="nothing">Select One</option>
<option value="Toni">Toni</option>
<option value="Jack">Jack</option>
<option value="James">James</option>
</select>
<div class="singer_profile_overview" id="Toni">toni</div>
<div class="singer_profile_overview" id="Jack">jack</div>
<div class="singer_profile_overview" id="James">james</div>
jquery:
jQuery(document).ready(function($){
$("#about_performance_lead_singer").change(function () {
var $this = $(this);
$('.singer_profile_overview').removeClass('visible');
$('#' + $this.val()).addClass('visible');
});
});
Couple of notes:
It is a good practice to store $(this) in a local variable, I usually call it $this.
You had an upper case / lower case problem in your original code. The id of the class to update needs to match the id you call in your selector
Remove the visible class from every <div> prior to applying one.
And there is still one problem, if you click your dropdown and select the first option, the event will not fire because nothing changed. That is why I added a 'Select One' option.
And here is a fiddle.
Try this. You also need to convert the value to lowerCase() to get the exact match.
<select name="about_performance_lead_singer" id="about_performance_lead_singer" value="">
<option value="Toni">Toni</option>
<option value="Jack">Jack</option>
<option value="James">James</option>
</select>
<div class="singer_profile_overview" id="toni">toni</div>
<div class="singer_profile_overview" id="jack">jack</div>
<div class="singer_profile_overview" id="james">james</div>
<style>
.singer_profile_overview
{
display:none;
}
.visible
{
display: block
}
</style>
<script>
jQuery(document).ready(function($){
$("#about_performance_lead_singer").change(function () {
$('.singer_profile_overview').removeClass('visible'); // hide all divs by removing class visible
$('#' + $(this).val().toLowerCase()).addClass('visible'); // find the matching div and add class visible to it
});
});
</script>
Example : http://jsfiddle.net/RxguB/204/
It is because the value of the dropdown are capitalized, while your ids are lowercase. Consider the following:
jQuery(document).ready(function($){
$("#about_performance_lead_singer").change(function () {
$('#'(this).val().toLowerCase()).addClass('visible');
});
});
Or alternatively, you can manually change the values to lower case, or the ids to be capitalized to match the values.
To answer the question as it was asked:
$("#about_performance_lead_singer").on("change", function(){
$(".singer_profile_overview").removeClass("visible");
$("#" + $(this).val().toLowerCase()).addClass("visible");
});
However, if you're only using the class "visible" to toggle their display, you can replace
removeClass("visible")
with
hide()
and
addClass("visible")
with
show()
Use the :selected selector to find the option that was selected. We can get its value by calling the prop method and passing in value as an attribute. Since the ids we are trying to get are lowercase, we use the toLowerCase() method.
The div we're trying to get is of the class .singer_profile_overview. We should use the filter function to get the ids. We store the result of this filter in a jQuery variable and finally add a class to it.
$("#about_performance_lead_singer").change(function () {
var selectedValue = $(":selected").prop("value").toLowerCase();
var selectedDiv = $(".singer_profile_overview").filter(function() {
return $(this).prop("id") == selectedValue;
});
selectedDiv.addClass('visible');
});
.visible {
color: gold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="about_performance_lead_singer" id="about_performance_lead_singer" value="">
<option value="Toni">Toni</option>
<option value="Jack">Jack</option>
<option value="James">James</option>
</select>
<div class="singer_profile_overview" id="toni">Toni</div>
<div class="singer_profile_overview" id="jack">Jack</div>
<div class="singer_profile_overview" id="james">James</div>
I have a jQuery code that displays a div upon selection of an option in a dropdown list. It is working fine but not as I need. the problem is that upon selecting the option, the page refreshes as there is an onchange="submit()" on the select. That makes the div appears for about half second and then disappear again when the page is loaded
Sample HTML:
<select onchange="submit()">
<option id="one" value="something">Car</option>
<option id="two" value="anything">Plane</option>
</select>
<div id="somediv" style="display:none;">This is a string.</div>
jQuery:
$('select').change(function(){
decide($(this));
});
var decide = function (elem) {
var touch = elem;
if (touch.val() === 'anything') {
return $('#somediv').css('display', 'block');
}
};
I want the div to be displayed when the option of id="two" and value="anything" is selected, and to remain displayed after the page refreshes. When the user selects the other option, the div disappears and remains hidden after the page refreshes. How can I achieve this please? All answers would be greatly appreciated.
You should remove the submit() from the onclick and submit after you call the decide function
Something like:
<select>
<option id="one" value="something">Car</option>
<option id="two" value="anything">Plane</option>
</select>
<div id="somediv" style="display:none;">This is a string.</div>
JS:
$('select').change(function(){
decide($(this));
$('form').submit();
});
var decide = function (elem) {
var touch = elem;
if (touch.val() === 'anything') {
return $('#somediv').css('display', 'block');
}
};
I'd suggest the following approach:
// define the function:
function showIfSelected() {
// 'this' is passed in by jQuery, and is the <select> element,
// this.options is a NodeList of the <option> elements of
// the <select>, this.selectedIndex is the index of the selected
// <option> from amongst that NodeList:
var opt = this.options[this.selectedIndex];
// finding all <div> elements with a 'data-showif' attribute:
$('div[data-showif]')
// hiding them all:
.hide()
// filtering that collection of <div> elements, to find the one
// whose 'data-showif' attribute shares the value with the <option>:
.filter('[data-showif=' + opt.value + ']')
// showing that <div>:
.show();
}
// binding the showIfSelected() function as the change-event handler:
$('select').on('change', showIfSelected)
// triggering the change event (to show/hide the correct <div>
// on document ready:
.change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option id="one" value="something">Car</option>
<option id="two" value="anything">Plane</option>
</select>
<div id="somediv" data-showif="anything">This is a string.</div>
References:
CSS:
Attribute selectors.
*jQuery:
change().
on().
filter().
hide().
show().
If you don't want to use ajax to submitting your data, you should pass selected value via get/post or store it in a session/cookie then retrive it when the page loads.
Instead, if you use ajax you could do something like this
$('select').change(function(){
decide($(this));
$('form[name=a]').submit();
})
var decide = function (elem) {
var touch = elem;
if (touch.val() === 'anything') {
return $('#somediv').css('display', 'block');
}
}
$('form[name=a]').submit(function(){
$('#res').text('Submitted!');
// Put $.ajax to submit your data
return false; // Commenting this will result in a page refresh
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="a" method="post">
<select>
<option id="one" value="something">Car</option>
<option id="two" value="anything">Plane</option>
</select>
</form>
<div id="somediv" style="display:none;">This is a string.</div>
<div id="res"></div>
Try this code,it may works.
<option id="one" value="First">Car</option>
<option id="two" value="Second" selected="selected">Plane</option>
</select>
<div id="div1">This is a string</div>
jQuery:
$(document).ready(function () {
$("select").change(function () {
var v = $("select").val();
if (v == "Second") {
$("#div1").show();
}
else {
$("#div1").hide();
}
});
});
How can i hide and show a div when click on the basis of text box. i want to show these type of result,the cursor pointer is inside the textbox the div will shown otherwise the focus is out the div will be hidden and if any value is inside a textbox the div will shown if the value is cleared the div will be hidden .these are the code
<div class="banner_search_inner_box_search">
<input type="text" name="search" class="search" id="searchid" onkeyup="getshows();" value="" placeholder="Enter a City,Locality" autocomplete="off">
<div id="result"></div>
</div>
<div id="drope_box" style="display:none;">
<div class="banner_search_type">
<select id="property_type" name="property_type">
<option value="All">All</option>
<option value="Apartment">Apartment</option>
<option value="Plot">Plot</option>
</select>
</div>
<div class="banner_search_price_min">
<select name="price_min" class="search_list" id="price_min">
<option value="">Price Min</option>
<option value="100000">1 lac</option>
<option value="1000000">10 lacs</option>
</select>
</div>
<div class="banner_search_price_max">
<select name="price_max" id="price_max">
<option value="">Price Max</option>
<option value="100000">1 lac</option>
<option value="1000000">10 lacs</option>
</select>
</div>
</div>
here is my js code
<script type="text/javascript">
function getshows()
{
if(document.getElementById("searchid").value != "")
{
document.getElementById("drope_box").style.display="block";
}
else
{
document.getElementById("drope_box").style.display="none";
}
}
</script>
<script type="text/javascript">
$('#searchid').blur(function() {
$("#drope_box").hide()
});
$('#searchid').focus(function() {
$("#drope_box").show()
});
</script>
somebody please help me
Here: http://jsfiddle.net/Kq9pX/
JS
Do it on load: Wrap around document.ready function.
$('#searchid').blur(function() {
if($(this).val() != ""){
$("#drope_box").show();
}
else{
$("#drope_box").hide();
}
});
$('#searchid').focus(function() {
$("#drope_box").show();
});
It's generally advised to attach events using jQuery's on rather than blur(), focus(), click() etc in newer versions of jQuery, although I admit in this case there is no difference (read more here Why use jQuery on() instead of click()). But caching jQuery objects by storing them in a variable, like below, is slightly faster.
var $search = $('#searchid');
var $dropBox = $('#drope_box');
$search.on('blur', function () {
$dropBox[($.trim($search.val()).length > 0 ? 'show' : 'hide')]();
}).on('focus', function () {
$dropBox.show();
});
There's no problem with show and hide other than when it's showed the div hides if you try to select the options, to turn this around we can use focusout on the drope_box div:
$(document).ready(function(){
$('#drope_box').focusout(function() {
//check if the text input and selects have values selected
if($("#searchid").val() == "" &&
$("#property_type").val() == "" &&
$("#price_min").val() == "" &&
$("#price_max").val() == "" )
$("#drope_box").hide() //if not, hides
});
$('#searchid').focus(function() {
$("#drope_box").show()
});
});
Of course, for it to work the:
<option value="All">All</option>
Must be cleared:
<option value="">All</option>
FIDDLE: http://jsfiddle.net/e672Y/1/
Use jQuery:
$("#textbox").mouseenter(function(){
$("#div").show();
});
$("#textbox").mouseleave(function(){
$("#div").hide();
});
This so far will show the div whilst it is hovered, mouseenter checks if the textbox is moused over, mouseleave checks if it is not.