Few things about form in HTML/CSS - javascript

I have a form. I tried to change styling of checkbox and radio buttons using CSS, but with no luck. Finally I found some JS code that seems to be working. It changes styling of checkboxes, but doesn't change styling of radio buttons. Also the position of labels of checkboxes does not assign with checkboxes.
JS code:
$(function() {
$('input[type=checkbox]').each(function() {
var span = $('<span class="' + $(this).attr('type') + ' ' + $(this).attr('class') + '"></span>').click(doCheck).mousedown(doDown).mouseup(doUp);
if ($(this).is(':checked')) {
span.addClass('checked');
}
$(this).wrap(span).hide();
});
function doCheck() {
if ($(this).hasClass('checked')) {
$(this).removeClass('checked');
$(this).children().prop("checked", false);
} else {
$(this).addClass('checked');
$(this).children().prop("checked", true);
}
}
function doDown() {
$(this).addClass('clicked');
}
function doUp() {
$(this).removeClass('clicked');
}
});
It doesn't seem to work perfectly in jsfiddle, but: http://jsfiddle.net/Sgn6n/1/
And here's the code I based my code on: http://jsfiddle.net/jtbowden/xP2Ns/

Related

Enable Dropdown Selection Using a Checkbox, Toggle Dependent Dropdown and DIV Visibility

I have an interesting scenario involves using a checkbox to enable the a dropdown. Upon changing the status of the 1st dropdown, it should enables the 2nd dropdown. Once a selection is made in the 2nd dropdown, it toggles the visibility of 2 hidden DIVs. Like the image below:
I have a prototype JSFIDDLE that I've been working on but there are a few issues with the script, such as:
1- Despite the dropdowns contain the "disabled" attribute, which should be enabled only once the checkbox is checked, but it only works if you click the checkbox 2 times. Here's the script:
var $checkBox = $('#mondayTransfer'),
$select = $('#mondayOptions');
$checkBox.on('change',function(e){
if ($(this).is(':checked')){
$select.removeAttr('disabled');
}else{
$select.attr('disabled','disabled');
}
});
2- The 2nd Selection dropdown should also be disabled; changes to the 1st Selection dropdown would enable it. Here's the script:
$(function(){
$('select').change(function(){
if($(this).attr('id') == 'mondayOptions' && $(this).val() == 'Default'){
$('select').not(this).prop('disabled', true).val('Disabled');
} else {
$('select').not(this).removeProp('disabled');
$('select option').removeProp('disabled');
$('select').each(function(){
var val = $(this).val();
if(val != 'Default' || val != 'Disabled'){
$('select option[value="'+val+'"]').not(this).prop('disabled', true);
}
});
}
});
});
3- Once the checkbox is unchecked, all the dropdowns should be disabled.
I would appreciate any help.
I refactored you code a little bit, you can test it here
let $checkBox = $('#mondayTransfer');
let divClasses = ['.ach', '.flash'];
let selects = ['#mondayOptions', '#box_g2'];
let setDisplayNone = function(className) {
divClasses.forEach(function(className) {
$(className).css("display", 'none');
});
}
$checkBox.on('change',function(e){
if ($(this).is(':checked')){
$('#mondayOptions').removeAttr('disabled');
} else {
// Disable both selects when mondayOptions is CHECKED
$('select').attr('disabled','disabled');
// Loop through each div you can select and set its display none
setDisplayNone(divClasses)
// Loop each select you have and then select the "selected" option
selects.forEach(function(className) {
$(className).val('selected');
});
}
});
$(document).ready(function() {
// When mondayOption is changed enable the second drop-down
$("#mondayOptions").change(function() {
$("#box_g2").attr("disabled", false)
});
// When the second drop-down changes its value
$("#box_g2").change(function() {
// set display none to all toggleDiv
setDisplayNone(divClasses)
// fetch the value selected
let className = $(this).val();
// use jquery to select the div and set the display yo block
$('.' + className).css('display', 'block')
});
});
If you remove all the code from //Enable drop-down Selection section of your snippet you would get part of the behaviour that you wanted to.
Anyhow here is how it is working now:
When the checkbox is CHECKED:
Enable the first drop-down.
When the checkbox is IS NOT CHECKED:
Disabled both selects
Make all .divToggle not visible by using the setDisplayNone function
Reset the value of the selects to the default value("selected" in your case)
When you select a value on the second drop-down:
Make all .divToggle not visible by using the setDisplayNone function
fetch the value selected
use jquery to select the div and set the display yo block
Check this working demo: http://jsfiddle.net/wth8mrLa/
Updated some of your scripts;
1) moved the scripts inside DOM ready
2) added select box2 variable $select2 = $('#box_g2');
3) when checkbox uncheck, disable both select boxes, reset null values and hide div
the rest scripts are the same.
JQUERY
//Toggle DIV Visibility Using the 2nd Dropdown Selection
$(document).ready(function() {
$("select").change(function() {
$(this).find("option:selected").each(function() {
var optionValue = $(this).attr("value");
if (optionValue) {
$(".divToggle").not("." + optionValue).hide();
$("." + optionValue).show();
} else {
$(".divToggle").hide();
}
});
}).change();
//Toggle 1st Selection Dropdown Once Checkbox is Checked
var $checkBox = $('#mondayTransfer'),
$select = $('#mondayOptions'),
$select2 = $('#box_g2');
$checkBox.on('change', function(e) {
if ($(this).is(':checked')) {
$select.prop('disabled', false);
} else {
$select.val('').prop('disabled', true);
$select2.val('').prop('disabled', true);
$(".divToggle").hide();
}
});
//Enable DropDown Selection
$(function() {
$('select').change(function() {
if ($(this).attr('id') == 'mondayOptions' && $(this).val() == 'Default') {
$('select').not(this).prop('disabled', true).val('Disabled');
} else {
$('select').not(this).removeProp('disabled');
$('select option').removeProp('disabled');
$('select').each(function() {
var val = $(this).val();
if (val != 'Default' || val != 'Disabled') {
$('select option[value="' + val + '"]').not(this).prop('disabled', true);
}
});
}
});
});
});
All of your js codes must be inside the document.ready() functions.
$(document).ready(function() {
//paste your code here....
});

Jquery previous .html() element still showing when new element clicked

I have to write code which finds all anchors and attaches a function that displays a popup of the elements text. My code is probably a mess, but I was able to get it working however the issue I have now is:
If I click link 1, then link 2, then click link 1 again, it displays link 2's text however if i click it again it displays the correct text.
I am not sure exactly how to rewrite or go about fixing this code to properly display the element which is clicked, text all the time.
here is a jsfiddle example: http://jsfiddle.net/2aLfL/1/
$(document).ready(function() {
function deselect(e) {
$('.pop').slideFadeToggle(function() {
e.removeClass('selected');
});
}
$(function() {
$('a').click(function(){
if($(this).hasClass('selected')){
deselect($(this));
} else {
$(this).addClass('selected');
$('.pop').slideFadeToggle();
var elText = $(this).text();
$('#elPop').html("<p>" + "<br><br>" + "You just clicked: <br><b>" + elText + "</b><br><br><br>" + "Click anywhere to Close" + "</p>");
console.log(this);
$("#closeWin").click(function () {
$('.anchorpop').hide();
});
}
return false;
});
});
$(function close(){
$(document).click(function(){
$('.anchorpop').hide();
});
});
$.fn.slideFadeToggle = function(easing, callback) {
return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
};
});
You're binding the following click handler
$("#closeWin").click(function () {
$('.anchorpop').hide();
});
inside <a> click handler so whenever a link is clicked, multiple handlers are being added.
You can avoid many unnecessary code using toggleClass() method.
You can also bind same event handlers to multiple elements by passing additional selectors.
after all your code boils down to
$(function () {
$('a').click(function () {
var htmlString = "<p>" + "<br><br>" + "You just clicked: <br><b>" + $(this).text() + "</b><br><br><br>" + "Click anywhere to Close" + "</p>"
$('.pop').html(htmlString).slideFadeToggle(function () {
$(this).toggleClass('selected');
});
return false;
});
$("#closeWin, .anchorpop").click(function () {
$('.anchorpop').hide();
});
});
and the custome slideFadeToggle function.
Updated Fiddle

jQuery button. Remove when clicking icon

Im using jQuery ui button to create divs that look like buttons dynamically. Clicking on these divs should open a dialog and clicking its icon should remove the div(button). Ive tried several different approaches but I cant seem to get the result I want.
Closest thing Ive achieved is by using onclick on both the icon & on the div itself, but the problem is that when clicking the icon I would first call the icon's onclick and then afterwards calling the div's onclick, which will cause the dialog to open after the div has been removed.
Ive also tried to add a disable property and set it to true on the div inside the icon's onclick and check for that inside the div's onclick but that dont work(I kinda get why.)
So my question is then: How can I create a button that will open a dialog when clicked on and with a icon that, when clicked on, removes the button?
Code:
function Add(value) {
var buttonid = "SearchResultBox" + ($("#SearchBoxAddedSearches .SearchResultBox").length + 1);
$("#SearchBoxAddedSearches").append("<div id='" + buttonid + "' class='SearchResultBox' onclick='ButtonClicked(this);'>" + value + "</div>");
$("#SearchBoxTextField").contents().filter(function () { return this.nodeType === 3; }).remove();
$('.SearchResultBox').button({
icons: {
primary: "ui-icon-circle-close"
}
}).delegate("span.ui-icon-circle-close", "click", function () {
var btnId = $(this).closest("div").remove().attr("aria-controls");
$("#" + btnId).remove();
});
$('.ui-icon-circle-close').attr('onclick', 'IconCloseClicked(this);');
}
function IconCloseClicked(value) {
$(value).parent().prop("disable", "true");
//alert($(value).parent().attr("id"));
alert("icon");
Remove($(value).parent());
}
function ButtonClicked(o) {
var test = $(o).prop("disable");
alert("div");
if ($(o).attr("disable") == undefined) {
Opendialog();
}
}
function Remove(value) {
$(value).remove();
}
function Opendialog() {
$("#dialog").dialog("open");
}
Ps. Reason why Ive used the button is because it is the widget that looks the most like what I want in jquery ui.
Updated(What I ended up with):
function Add(value) {
var buttonid = "SearchResultBox" + ($("#SearchBoxAddedSearches .SearchResultBox").length + 1);
$("#SearchBoxAddedSearches").append("<div id='" + buttonid + "' class='SearchResultBox'>" + value + "</div>");
$("#SearchBoxTextField").contents().filter(function () { return this.nodeType === 3; }).remove();
$('.SearchResultBox').button({
icons: {
primary: "ui-icon-circle-close"
}
}).click(function (e) {
Opendialog();
});
$('.ui-icon-circle-close').click(function (e) {
$(this).parent().remove();
e.stopPropagation();
});
}
function Opendialog() {
$("#dialog").dialog("open");
}
I'm assuming the icon is a child element of the button div. When the icon is clicked, you need to stop the click event bubbling to the parent div. You can do this with event.stopPropagation()
$('.icon').click(function(e){
e.stopPropagation();
});

Jquery toggle background color

I have a jquery function that when a li is clicked, the li expands. That part is working fine. Now, I want, when the li is clicked it toggles a background color. But it works, however when i have to click on the li item again to untoggle the background color. Can someone assist me in the right direction on how to achieve this.
$(function() {
$('.a').click(function() {
var name = $(this).attr("name");
var content = $('.content[name=' + name + ']');
$('.content').not(content).hide('fast');
$('.selected').css('background', 'yellow');
content.slideToggle('fast');
});
$("li").click(function() {
$(this).toggleClass("highlight");
});
});​
On every click set your <li>-s to default color and highlight the current:
$("li").click(function() {
$("li").removeClass("highlight");
$(this).addClass("highlight");
});
...
UPDATE
http://jsfiddle.net/NXVhE/4/
$(function() {
$('.a').click(function() {
$(this).removeClass("highlight");
var name = $(this).attr("name");
var content = $('.content[name=' + name + ']');
$('.content').not(content).hide();
content.toggle();
});
$("a").click(function () {
$("a").removeClass("highlight");
if ( $(".content").is(":visible") ) {
$(this).addClass("highlight");
}
});
});
Assuming the <li>s are all siblings, it would be slightly more efficient to do something like this, and would allow for more than one list on the same page to function independently of one another (again, assuming that is the desired functionality)
$('li').click(function() {
$('this').addClass('highlight').siblings().removeClass('highlight').
});

Why isn't my checkbox change event triggered?

I have two functions.
The first function translates a div click into a checked/unchecked toggle.
The second function translates a checkbox change into a hide/show event.
The problem is that when I use the first function to check/uncheck the box, the second function is not called. I am new to javascript, thanks.
<script type="text/javascript">
$(document).ready(function() {
$(":checkbox").parent().click(function(evt) {
if (evt.target.type !== 'checkbox') {
var $checkbox = $(":checkbox", this);
$checkbox.attr('checked', !$checkbox.attr('checked'));
evt.stopPropagation();
return false;
}
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$(":checkbox").change(function() {
if($(this).attr("checked")) {
$('.'+this.id).show();
}
else {
$('.'+this.id).hide();
}
});
});
</script>
The change event does not fire when you programmatically change the value of a check box. What you can do to ensure it fires is:
$(":checkbox").parent().click(function(evt) {
if (evt.target.type !== 'checkbox') {
var $checkbox = $(":checkbox", this);
$checkbox.attr('checked', !$checkbox.attr('checked'));
$checkbox.change();
}
});
Don't bother with the first snippet. Just use LABEL elements:
<label><input type="checkbox">Some option</label>
Now, when the user clicks the label (the text next to the checkbox), the checkbox will be activated.
The second snippet can be optimized:
$('input:checkbox').change(function() {
$('#' + this.id).toggle(this.checked);
});
you are using '.' which is for class selectors instead use '#' since you are using the element ID. Like this:
$(document).ready(function() {
$(":checkbox").bind('change', function() {
if($(this).attr("checked")) {
$('#'+this.id).show();
}
else {
$('#'+this.id).hide();
}
});
});

Categories