So i want to change css class in HTML with checkbox. I know how boolean works, but how can i change text. For example, if checkbox is checked i want to inject "pull-right" class, and if it's not, then "pull-left" is defaulf.
I tried few things, but it wont work. Anyone knows how to create this?
You can try doing it like this:
Sample HTML:
<div id="container" class="pull-left">Some Content </div>
<input type="checkbox" id="mybox"/>
Sample Javascript
$("#mybox").on("change", function(){
$('#container').toggleClass("pull-left").toggleClass("pull-right");
})
Working sample on jsfiddle
I don't see any jquery tag to your question. so let me give you pure javascript solution:
fiddle: http://jsfiddle.net/Nz8Tq/
html:
<input type="checkbox" name="change" id="change"/>change class
<div class="pull-left" id="container"></div>
javascript:
document.getElementById("change").onchange = function(){
if(this.checked){
document.getElementById("container").setAttribute("class","pull-right");
} else {
document.getElementById("container").setAttribute("class","pull-left");
}
}
CSS only Demo
The :checked pseudo-class in CSS selects elements when they are in the selected state. It is only associated with input <input> elements of type radio and checkbox . The :checked pseudo-class selector matches radio and checkbox input types when checked or toggled to an on state. If they are not selected or checked, there is no match.
Browser Support
Chrome Safari Firefox Opera IE Android iOS
Any 3.1+ Any 9+ 9+ any any
source
CSS
input[type=checkbox] ~ div { /* I used general sibling selector u can use other css selectors according to ur requirement like adjacent sibling, child selector etc */
color: #ccc;
font-style: italic;
float: left
}
input[type=checkbox]:checked ~ div {
color: #f00;
font-style: normal;
float: right
}
input[type=checkbox] {
position: fixed;
}
HTML
<input type="checkbox" id="chkbx" name="chkbx">
<br>
<div for="chkbx">The power of css</div>
You can do it easily by doing this:
Jquery
$(function(){
$("input[name=toggle-checkbox]").on("change", function(){
$(".test").toggleClass("pull-left").toggleClass("pull-right");
});
});
Then in your html add this:
<input name="toggle-checkbox" type="checkbox" />
<div class="test pull-left"></div>
And a bit of css to see the effect:
.test{
display:block;
width:100px;
height:100px;
}
.pull-left{
background-color:red;
}
.pull-right{
background-color:yellow;
}
I hope this helps!!! :)
Related
I need help on my php program.
I have 2 forms inside my testing.php
Main form named "mainform"
the other Form is with checkbox named "modalForm"
My program works like this. If you click the disabled textbox, a modal will pop-up containing the form with checkbox. If only 1 of the checkbox were check I'm gonna display the textlabel of it on my disabled textbox otherwise if more than 2 were check I will display the value "Multiple" on the disabled textbox.
In short I will display only the textlabel on UI while its value will be inserted/updated into the system database.
I tried lots of condition and still i dont get it. anyone help me please.
Here is my look a like code
testing.php
<form method="post" name="mainform" action="">
<label>TestLabel</label>
<a href="#modal"> <!-- when the input textbox was clicked, modal will pop up -->
<input disabled type="text" name="test" placeholder="test" value="">
</a>
</form>
<form method="post" name="modalForm" id="modalForm" action="testing.php">
<div class="modalwrapper" id="modal"> <!-- modal -->
<div class="modalcontainer">
<div class="modalcol1">
<label>Test 1</label>
<input type="checkbox" name="test_modal[]" value="mark">
<div class="clear"></div>
<label>Test 2</label>
<input type="checkbox" name="test_modal[]" value="joe">
<div class="clear"></div>
<label>Test 3</label>
<input type="checkbox" name="test_modal[]" value="kevin">
<div class="clear"></div>
<label>Test 4</label>
<input type="checkbox" name="test_modal[]" value="michael">
<div class="clear"></div>
<label>Test 5</label>
<input type="checkbox" name="test_modal[]" value="jordan">
<div class="clear"></div>
<div class="savebutton">
<input class="btn1" type="submit" value="Submit"/>
</div>
</div>
</div>
<div class="overlay"></div>
</div>
</form>
styles.css
/* modal layout */
.modalwrapper {
top: 0;
left: 0;
opacity: 0;
position: absolute;
visibility: hidden;
box-shadow: 0 3px 7px rgba(0,0,0,.25);
box-sizing: border-box;
transition: all .4s ease-in-out;
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .4s ease-in-out;
}
.modalwrapper:target {
opacity: 1;
visibility: visible
}
.overlay {
background-color: #000;
background: rgba(0,0,0,.8);
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
.modalcontainer {
display: table;
background-color: #777;
position: relative;
z-index: 100;
color: #fff;
padding: 5px;
}
.modalcol1 { display: table-cell; }
.clear { clear: both; }
.modalwrapper input[type=checkbox] {
float: right;
margin-right: 20px;
}
.savebutton input[type=submit] {
float: right;
background-color: maroon;
color: #fff;
border: none;
padding: 5px 10px;
margin-top: 5px;
margin-right: 20px;
}
/* modal layout ends here */
Hope you guys can help me. I wanna know how can I pass the value of the checkboxes on my mainform . Thank you so much in advance.
I tried that code,set id="submit" at the submit button of the modal form and id="test_modal[]" at checkboxes,I used some functions of jquery I set at header the <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(e) {
// var checked = [];
i=0;
temp=""
$("input[name='test_modal[]']:checked").each(function ()
{ temp=$(this).val();
i++;
// checked.push($(this).val());
});
if (i==1)
$("#test").val(temp);
else if (i>1)
$("#test").val('multiple');
$("#modal").fadeOut();
// alert($("#do").val())
e.preventDefault();
});
});</script>
Here is the sample for couple of cases that you may find usefull for your problem.
PHP variable to Javascript variable:
<?php $myvar=10; ?>
<script type="text/javascript">
jsvar = <?php echo $myvar; ?>;
document.write(jsvar); // Test to see if its prints 10:
</script>
Form variable to Javascript variable:
<form name="myform4"> <input type="hidden" name="formvar" value="100"> </form>
<script type="text/javascript">
jsvar = document.myform4.formvar.value;
document.write(jsvar) // test
</script>
PHP variable to Form variable:
<form name="myform4">
<input type="hidden" name="formvar" value="<?php $phpvar=10; echo $phpvar; ?>"> // PHP code inside HTML!!
</form>
Javascript variable to Form variable:
<form name="myform3">
<!-- It needn't be a "hidden" type, but anything from radio buttons to check boxes -->
<input type="hidden" name="formvar" value="">
</form>
<script type="text/javascript">
jsvar=10;
document.myform3.formvar.value = jsvar;
</script>
JavaScript:
You can solve this problem just with JS. To do so, you have to replace you action-Attribute in the form-Tag to action="#". To read more about the hashtag there, look up this link.
After that, you have to call a JS-Function when the Form gets submitted. YOu can do this with adding onSubmit="countCheckboxes()" to the <form>-Attribute. This means, that if the form gets submitted, this JS-Function gets executed. Now, you just have to create a new JS-Function called countCheckboxes(). There, you can count the selected checkboxes with this line:
alert(document.querySelectorAll('input[type="checkbox"]:checked').length);
Check now, if there is just one selected checkbox or more and set the value of the <input>.
Tip:
Get the value of the only checked checkbox, you have to restructure your HTML like this:
<label>
Step 1
<input type="checkbox" ... />
</label>
<label>
Step 2
...
And then get the (first and only) text in the label with the checked box like this:
document.querySelectorAll('input[type="checkbox"]:checked')[0].parentElement.textContent
jQuery:
To open and close your modal you should use JS. I recommend you to use the jQuery library (Check out this article to learn how to use jQuery in your JS). With that, you can simply open and close the modal with .fadeIn() and .fadeOut().
In fact, here we have another problem: Normally in jQuery, you can check if something got clicked with this code:
$("#idOfYouElement").click(function() {
// this code gets executed if the element got clicked
});
But in your case, the input is disabled and doesn't fire a clickevent and this results that the .click()-Function also doesn't get executed. To fix this, we place a <div> over the input, which is transparent so the users don't see it. Then we add the .click()-Function to this div and not the input. This would look like this:
HTML:
<form method="post" name="mainform" action="">
<label>TestLabel</label>
<input type="text" id="inpCheckboxes" name="test" placeholder="test" value="" disabled >
</form>
JavaScript:
$("#inpCheckboxes").click(function(){
$("#idOfYourModal").fadeIn();
});
After this, we have to close the modal after the submit-button got clicked. There are multiple solutions for that, for me, this one using .submit() is the cleanest:
$("#idOfYourForm").submit(function() {
$("#idOfyourModal").fadeOut(); // Since your Modal is also your Form, you could also use here $(this).fadeOut();
});
You also want to count all your checked checkboxes. You can combine this with your function above, since you want to count the checkboxes when the user hits 'submit'. The jQuery-way to count theboxes would be:
var l = $("input[type='checkbox']:checked").length;
With this value, you can now use a if else to set the value of your <input>-Field (remember to remove now the disabled-Attribute from it).
I was wondering if there is another way in PHP or HTML to use forms without having to have check marks or radio buttons .. For example , if I just click on a link or picture , it can send data to the other PHP file according specified in form action ... I'd like to know if I can just make picture icons send data to the other php page in form action without having to use radio buttons .. but I guess if someone only has to choose one icon from many available .. radio buttons could be the only option available or am I wrong ?
I like to use the button element. Throw in a type="submit", and you have yourself a party.
Demo: http://jsfiddle.net/RnYeg/
HTML
<form method="post" action="">
<button type="submit" name="animal" value="pig">Go</button>
</form>
CSS
button {
background: transparent url(http://lorempixel.com/output/animals-q-c-200-200-6.jpg) 0 0 no-repeat;
border: none;
cursor: pointer;
height: 200px; width: 200px;
}
Alternatively, you could just use an img tag:
Demo: http://jsfiddle.net/umuaU/2/
HTML
<form method="post" action="">
<button type="submit" name="animal" value="pig"><img src="http://lorempixel.com/output/animals-q-c-200-200-6.jpg" alt="Pig" /></button>
</form>
CSS
button {
background: none;
border: none;
cursor: pointer;
margin: 0;
padding: 0;
}
currently revising my code for my menu bar just want to ask on how to change the class of my hr tag wherein whenever I click a link the line below the link will change. Well I'm doing it for my horizontal menu. I've made the some code but I admit I'm not so sure of what I'm doing. Well here's my code so far:
HTML
<div class="menu">
<table class="menu_item">
<tr>
<td>
Events
<hr class="active" />
</td>
<td>
Reports
<hr />
</td>
<td>
Settings
<hr />
</td>
<td>
Logout
<hr />
</td>
</tr>
</table>
</div>
<script type="text/javascript" src="<?php echo $base; ?>javascripts/anim.js"></script>
CSS
.menu{
margin-top:20px;
margin-bottom:10px;
}
.menu_item td{
text-align:center;
width:25%;
}
.menu_list{
width:1000px;
margin-left:auto;
margin-right:auto;
}
.active{
background-color:#330000;
}
a {
color:#ffffff;
text-decoration:none;
outline:0;
border:0;
}
hr.active{
color:#ffff00;
}
and the Jquery
$("a").click(function() {
$("hr").click(function (){
$("hr").removeClass("active");
$(this).addClass("active");
});
});
Well I basically just want it to appear like the tabs on android ICS a little bit. Just a little bit though.
I'm not sure what Google ICS looks like, but I think this does what you're asking.
$("a").click(function() {
$("hr").removeClass("active");
$(this).next().addClass("active");
});
Demo here. I changed the CSS a bit.
In your code, the $("hr").click is unnecessary. The user does not click on the hr tag, only the a tag. Also, you were adding the active class to the a tag, not the hr.
$("a").click(function() {
$(this).next().toggle();
});
Thanks
take a look on the give example:
$('.menu_item a').each(function(i, e){
$(this).click(function(){
$('.menu_item hr').removeClass('active');
$(this).next().addClass('active');
});
});
here is the live working code: http://jsfiddle.net/jogesh_pi/EFmvJ/
Try the following
$('.menu a').click(function(){
//Selects hr tag below a tag and toggle active
$(this).next().toggleClass('active');
});
But you will also need to change your CSS .active class to the following to change an HR tag color
hr.active{
display: block; height: 1px;
border: 0; border-top: 1px solid #ffff00;
margin: 1em 0; padding: 0;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript Confirm popup Yes, No button instead of OK and Cancel
Please someone help me to make a confirm box with button "Yes" and "No" without using jquery or VB.Script. I have searched a lot but I got all of the answers with jquery but that is not my requirement.
I have to call another function from confirmbox function. The code I am using below
HTML
Delete
And Javascript
<script type="text/javascript">
function show_confirm(cat_id)
{
var conf = confirm("Are you sure you want to delete the file");
if (conf ==true)
{
deleteCatagory(cat_id);
}
}
function deleteCatagory(cat_id)
{
var obj = document.getElementById("file_"+cat_id);
callAjax(cat_id,obj);
}
</script>
Very simple. You'll have to custom code an HTML box that has a Yes and No button. The Yes button executes the code and hides the box, the No button just hides the box. Something as simple as this would do:
HTML
<div id="confirmation">
Are you sure you want to delete the category?<br>
<input type="button" onclick="deleteCategory(document.getElementById('catID').value);this.parentNode.style.display='none';" value="Yes">
<input type="button" onclick="this.parentNode.style.display='none';" value="No">
<input type="hidden" id="catID" value="0">
</div>
CSS
<style type="text/css">
<!--
#confirmation {
display: none;
position: absolute;
top: 50%;
left: 50%;
background-color: white;
border: 2px solid white;
padding: 3px;
text-align: center;
width: 200px;
height: 50px;
}
</style>
Updated show_confirm():
function show_confirm(catID) {
document.getElementById('catID').value=catID;
document.getElementById('confirmation').style.display='block';
}
You need to create it yourself, this: http://dev.sencha.com/deploy/ext-4.0.0/examples/message-box/msg-box.html seems to have a way to do it but this is from 5 minutes of googling. Others suggest using a div to do it, which is my personal preference.
If you're fine with the "Ok" or "Cancel" options the confirm box gives you, you only need to fix the typo in your call. I'd do it like this
Delete
If you however want to change the default text then you're out of luck with the default confirm popup. You'll have to come up with a html based "popup".
I'm using a jQuery plugin called jQtransform (http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/) which skins HTML form elements like the input box and submit buttons. All is well until I need to use an image as the submit button. I tried the following CSS code but the original button still appears, and not the image.
CSS:
/* this is the submit button */
#search {
width: 32px;
height: 32px;
padding: 0px;
margin: 0px;
border: 0px;
background: transparent url(../images/template/icons/search.png) no-repeat center top;
overflow: hidden;
cursor: pointer; /* hand-shaped cursor */
cursor: hand; /* for IE 5.x */
}
HTML:
<input type="submit" name="search" value="Search" id="search" />
** HTML code looks like it has been processed by the jQuery plugin when viewed in Chrome's 'Inspect Element' feature. The above is the original HTML code as seen when you select 'View Page Source' in Chrome.
What should I do to replace the submit button with my own image? I'm not too good with jQuery...
UPDATE
GREAT! All the answers are working. I must have been thinking too much :)
This should work
<form>
<input type="image" src="[some image]" onsubmit="submitForm();" id="search">
</form>
How about using good old HTML <input type="image" ... />?
<img type="image" src="img src..." id="search" />