I have two buttons on my page, I want it so that one starts in the down state display a piece of text in a div, then if the other button is pressed that button stays in the downstate displaying its piece of text in a div and making the first button in the normal state and hiding it's piece of text.
This is the sort of code I have been unsuccessfully working with;
$('#myButton').on('mousedown', function () {
$('#myButton').toggleClass('myButtonActivated',0);
document.getElementById('info1').style.zIndex = 1;
document.getElementById('info2').style.zIndex = 0;
})
Any guidance would be great, thanks.
this code may help you
<style>
.active
{
box-shadow: 3px 2px 0px #888888;
}
</style>
<script>
$(document).ready(function () {
$('.button').click(function () {
$(".button").removeClass("active");
$(this).addClass("active");
});
});</script>
<body>
<input class="button" type="button" value="1st button" />
<input class="button" type="button" value="2nd button" />
</body>
Could you try this:
$('#myButton').on('mousedown', function () {
$('#myButton').toggleClass('myButtonActivated',0);
if($('#myButton').hasClass(myButtonActivated)){
document.getElementById('info1').css('display','block');
document.getElementById('info2').css('display','none');
}else{
document.getElementById('info1').css('display','none');
document.getElementById('info2').css('display','block');
}
})
Since you're using JQuery, you might as well take advantage of more of its abilities.
Here's a working example: http://jsfiddle.net/rrKDd/1/
You can use jquery ui class for disabled style if you have it available:
<script>
var $button1 = $('#myButton'),
$button2 = $('#myButton2'),
$text1 = $('#info1'),
$text2 = $('#info2');
$button1.click(function () {
$button1.prop('disabled', true).addClass("ui-state-disabled");
$text1.show();
$button2.prop('disabled', false).removeClass("ui-state-disabled");
$text2.hide();
});
$button2.click(function () {
$button2.prop('disabled', true).addClass("ui-state-disabled");
$text2.show();
$button1.prop('disabled', false).removeClass("ui-state-disabled");
$text1.hide();
});
</script>
You can obviously choose your own CSS class to add/remove, and if you don't want them to appear disabled you can just do this:
<script>
var $button1 = $('#myButton'),
$button2 = $('#myButton2'),
$text1 = $('#info1'),
$text2 = $('#info2');
$button1.click(function () {
$button1.prop('disabled', true);
$text1.show();
$button2.prop('disabled', false);
$text2.hide();
});
$button2.click(function () {
$button2.prop('disabled', true);
$text2.show();
$button1.prop('disabled', false);
$text1.hide();
});
</script>
Related
i have this kind of structure...
<div class="copy_from">
<img src="images/1.png" />
</div>
<button class="copy_button">Copy</button>
<div class="copy_to">
</div>
i want to add the content of the copy_from div to copy_to div with an additional button "Add to Cart".
i am trying this...but it is not working...
<script type="text/javascript">
$(document).ready(function () {
$('.copy_button').click(function () {
var one1 = $('.copy_from').html();
var two = $(one1).append('<button>Add to Cart</button>');
$('.copy_to').html(one1);
});
});
</script>
Try
$('.copy_button').click(function () {
var one1 = $('.copy_from').html() + '<button>Add to cart</button>';
$('.copy_to').html(one1);
});
Or you could try changing this line
$('.copy_to').html(one1);
to ->
$('.copy_to').prepend(one1);
You've just got the order of things a bit mixed up. You make a copy of the html from .copy_from and then you add the button, afterwards. You can simply add the button to .copy_to afterward instead...
$(document).ready(function () {
$('.copy_button').click(function () {
var one1 = $('.copy_from').html();
$('.copy_to').html(one1).append('<button>Add to Cart</button>');
});
});
Title - Sorry about the title, it was difficult for me to actually explain this.
So I recently finished working on a dynamic fields system using jQuery. This is all working great however I'm wanting to re-use the html for the system over and over again on the same page, this causes problems.
Problem
- When you have duplicates of the form on the same page, and you press 'Add Field' it will run the function and apply the functions to the other classes on the page. (See fiddle for example.)
When you just have one form on the DOM it works fine, but I'm wanting to alter the html slightly so I can use it for different scenarios on a page. I don't want to have separate jQuery files to do this because I don't think it's necessary. I was thinking maybe I could target it's parent containers instead of the class directly? Then I could recycle the same code maybe?
Any suggestions on this guys?
HTML:
<form action="javascript:void(0);" method="POST" autocomplete="off">
<button class="add">Add Field</button>
<div class='input_line'>
<input type="text" name="input_0" placeholder="Input1">
<input type="button" class="duplicate" value="duplicate">
<input type="button" class="remove" value="remove">
</div>
</form>
JQUERY:
$(document).ready(function () {
'use strict';
var input = 1,
blank_line = $('.input_line'),
removing = false;
$('.remove').hide();
$('.add').click(function () {
var newElement = blank_line.clone(true).hide();
$('form').append(newElement);
$(newElement).slideDown();
$('.remove').show();
});
$('form').on('click', '.duplicate', function () {
$(this).parent().clone().hide().insertAfter($(this).parent().after()).slideDown();
$('.input_line').last().before($('.add'));
$('.remove').show();
input = input + 1;
});
$('form').on('click', '.remove', function () {
if (removing) {
return;
} else {
if ($('.input_line').length <= 2) {
$('.remove').hide();
}
$(this).parent().slideUp(function () {
$(this).remove();
removing = false;
});
$('.input_line').last().before($('.add'));
input = input - 1;
}
removing = true;
});
});
Working fiddle - JSFiddle
Problem fiddle - JSFiddle
As you can see in the problem fiddle above, when you duplicate the form it start conflicting. I would like each form to work independently.
Any help would be greatly appreciated!
You need to use closest('form') to find the associated form. Also when looking up the other fields, you need to search within the context of the related form, http://jsfiddle.net/95vaaxsL/7/
function addLine($inputLine) {
var $form = $inputLine.closest('form');
var $newElement = $inputLine.clone(true).hide();
$newElement.insertAfter($inputLine);
$newElement.slideDown();
$form.find('.remove').show();
}
$(document).ready(function () {
'use strict';
$('.remove').hide();
$('.add').click(function () {
addLine($(this).closest('form').find('.input_line:last'));
});
$('form').on('click', '.duplicate', function () {
addLine($(this).closest('.input_line'));
});
$('form').on('click', '.remove', function () {
var $inputLine = $(this).closest('.input_line');
var $form = $inputLine.closest('form');
if ($form.find('.input_line').length < 3) {
$form.find('.remove').hide();
}
$inputLine.slideUp(function(){
$inputLine.remove();
});
});
});
Pulled out the function.
My question might be a bit vague but here's what I have right now. Here's the fiddle I did
I need the More button to change background color to orange every time the orange background button is clicked and only if the More div is still at the bottom of the screen (The MORE button slides up when it's clicked)
And some snippet of my code:
<script>
$(document).ready(function(){
$('.chatbox').hide();
$('#btn-chat').click(function() {
$("#blk-collaboration .chatbox").slideToggle("slow",function(){
$("#blk-collaboration #toggle").css("background","transparent");
});
$(this)//this is a button DOM element. wrapping in jQuery object.
.toggleClass('wide'); // removing wide class so button became smaller.
});
});
function changeColor(color) {
document.getElementById("circle").style.backgroundColor = color;
}
document.getElementById("online").onclick = function() { changeColor("#92bf1e"); }
document.getElementById("offline").onclick = function() { changeColor("#747474"); }
document.getElementById("upcoming").onclick = function() { changeOrange("#f4b066"); }
</script>
Here are the buttons...
<input id="online" type="button" value="Online" />
<input id="offline" type="button" value="Offline" />
<input id="upcoming" type="button" value="Orange Background" />
Been trying to figure this out but the color of the more button still changes even if it's already slided up...
This should to do it, add this below the changeColor function:
function changeOrange(color) {
if ($('#btn-chat').hasClass('wide'))
document.getElementById("btn-chat").style.backgroundColor = color;
}
If the background is already orange, and the more button moves up, should the background change back to blue?
add this in your $(document).ready
$('input#upcoming').on('click', function(){
$('a#toggle').css({'background':'#f4b066'});
$('#btn-chat').css({'background':'#f4b066'});
})
working example
http://jsfiddle.net/vo0d9ubj/5/
Check the condition $("#blk-collaboration .chatbox").is(':visible') whether the More is toggled or not. Here i'm using the variable tempBottom to check the More div
Try this piece of Query
var tempBottom=true;
function changeColor(color) {
document.getElementById("circle").style.backgroundColor = color;
}
function changeOrange(color) {
if(tempBottom==true){
$('.wide').css("background-color", "#f4b066");
}
}
document.getElementById("online").onclick = function() { changeColor("#92bf1e"); }
document.getElementById("offline").onclick = function() { changeColor("#747474"); }
document.getElementById("upcoming").onclick = function() { changeOrange("#f4b066"); }
$(document).ready(function(){
$('.chatbox').hide();
$('#btn-chat').click(function() {
$("#blk-collaboration .chatbox").slideToggle("slow",function(){
$("#blk-collaboration #toggle").css("background","transparent");
if( $("#blk-collaboration .chatbox").is(':visible')){
tempBottom=false;}else
{ tempBottom=true;
}
});
$(this)//this is a button DOM element. wrapping in jQuery object.
.toggleClass('wide'); // removing wide class so button became smaller.
});
});
DEMO
In your case, here is how to do it...
$("#upcoming").on("click", function(){
// check if 'More' button's position is in between 100px from the bottom
if( ($("#toggle").offset().top >= ($(window).scrollTop() + $(window).height() - 100))) {
// change the color
$("center").css("background", "#eebe57");
}
});
FIDDLE
(Scroll to bottom of the Javascript section!)
I am trying to create a function that will toggle optional inputs if chosen, here is what I have done so far:
HTML:
<div>
<input>
<a class="input-toggle" href="#"><span>Toggle Option</span><span>Close</span></a>
<div class="input-toggle-content">
<input name="">
</div>
</div>
JavaScript:
$('.input-toggle').each(function() {
$(this).next("div").hide().end();
$("span:last-of-type").hide();
$(this).on('click', function() {
$(this).next("div").slideToggle();
$("span:first-of-type").hide();
$("span:last-of-type").show();
});
});
So, the way it should work is when clicked on .input-toggle the div that is just next to it will be toggled and if clicked again the div will go away... I got this bit working, however, I want to also toggle <span>Toggle Option</span> with <span>Close</span> and I can't get it working... I don't know if the way I structured my function is correct?
Try,
$('.input-toggle + div.input-toggle-content').hide();
$(".input-toggle span:last-of-type").hide();
$('.input-toggle').click(function () {
$(this).next('div.input-toggle-content').toggle();
var spans = $('span', this);
spans.not(spans.filter(':visible').hide()).show();
});
DEMO
here you go: http://jsfiddle.net/jPe3A/
$('.input-toggle').each(function() {
$(this).next("div").hide().end();
$("span:last-of-type").hide();
$(this).on('click', function() {
$(this).next("div").slideToggle();
if($("span:first").is(':hidden')){
$("span:first").show();
$("span:last").hide();
}
else{
$("span:first").hide();
$("span:last").show();
}
});
});
I'm testing javascript code for day/light background switch and I don't know how to do something. I'm newbie to javascript, so I'm learning new stuff.
So what I want to do?
When I click for example on button "Day" (which change background to yellow), I want that style for yellow background stay in the code after page is refreshed. I heard something about Cookies/LocalStorage, but I don't know how to implement it for this code.
Feel free to change whole code if you know easier way to do this, but please explain why it's better or why it should be like that.
Thanks in advance for your help.
Here is the code:
HTML:
<body id="body">
<input type="button" onclick="day();" value="Day" />
<input type="button" onclick="night();" value="Night" />
<input type="button" onclick="reset();" value="Reset" />
</body>
CSS:
.darkSwitch {
background: #808080;
}
.lightSwitch {
background: #ffff99;
}
JavaScript:
function day() {
body.className = "lightSwitch";
};
function night() {
body.className = "darkSwitch";
};
function reset() {
body.className = "";
};
$(function() {
var button = $('input[type=button]');
button.on('click', function() {
button.not(this).removeAttr('disabled');
$(this).attr('disabled', '');
});
});
Last edit: now disabling selected button on page load, CODE NOT IN THIS POST, see the latest JSFiddle
Explanation
What I did:
The code is put in between<script> tags at the end of the <body> (personnal preference)
I added the parameter event to the onClick event of the button element.
I added event.preventDefault() at the start of the onclick event of the button element: ensuring the page is NOT refreshed on the click of a button.
Warning: ALL the buttons will behave the same in your page. If you have other buttons, I suggest you add another class for those three buttons and bind the event on the button.myClass element.
I added a condition on the button state change, so the reset button won't get disabled.
eval($(this).val().toLowerCase()+"();"); gets the value of the the clicked button and executes the function attached to it.
Solution
HTML
<body id="body">
<input type="button" class="changeBg" onclick="day();" value="Day" />
<input type="button" class="changeBg" onclick="night();" value="Night" />
<input type="button" class="changeBg" onclick="reset();" value="Reset" />
</body>
JavaScript
(JSFiddle) <-- Check this out Updated with classes & cookies
function day() {
body.className = "lightSwitch";
};
function night() {
body.className = "darkSwitch";
};
function reset() {
body.className = "";
};
$(function () {
/* RegEx to grab the "bgColor" cookie */
var bgColor = document.cookie.replace(/(?:(?:^|.*;\s*)bgColor\s*\=\s*([^;]*).*$)|^.*$/, "$1");
var button = $('input[type=button].changeBg');
button.on('click', function (event) {
event.preventDefault();
/* Executing the function associated with the button */
eval($(this).val().toLowerCase() + "();");
button.not($(this)).removeAttr('disabled');
if ($(this).val() != "Reset") {
$(this).attr('disabled', '');
/* Here we create the cookie and set its value, does not happen if it's Reset which is fired. */
document.cookie = "bgColor="+$(this).val();
}
});
/* If the cookie is not empty on page load, execute the function of the same name */
if(bgColor.length > 0)
{
eval(bgColor.toLowerCase()+'()');
/* Disable the button associated with the function name */
$('button[value="'+bgColor+'"]').attr("disabled","disabled");
}
});
I recommend you don't use cookies unless localStorage is not supported. They slow your site down.
if(localStorage){
localStorage.setItem("bgColor", "lightSwitch");
}else{
document.cookie = "bgColor=lightSwitch";
}