<html>
<head>
<script type="text/javascript">
function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="block")
which.style.display="none"
else
which.style.display="block"
}
</script>
</head>
<body>
credit card
<a href="javascript:hideshow(document.getElementById('adiv123'))">
<input type="checkbox" />
</a>
<div id="adiv123" style="font:24px normal; style=display:block;">
check
<input type="text" name="check" class="styled"/>
</div>
</body>
</html>
The output of the program must be:
it should display the text when we check the checkbox and should hide the text when checkbox was unchecked.
In this case,when we open the output for the first time the text was displayed without checking the checkbox.
can anyone clarify why it was happening?
This happens because you don't run the hideshow function until you click on your checkbox (why did you wrap it into a link?). So after the pageload the adjacent div is always displayed, regardless of the state of the checkbox element.
Anyway, if you don't support IE8 and previous, you can achieve the same behaviour with css only. E.g.
html
<input type="checkbox" />
<fieldset>
<span>checked</span>
<input type="text" name="check" class="styled"/>
</fieldset>
Css
input + fieldset { display: none }
input:checked + fieldset { display: block }
have a look here: http://jsfiddle.net/Uhcxd/
CODE
document.getElementById("adiv123").style.display = "none";
document.getElementById("showHide").onchange = function(){
if(this.checked)
document.getElementById("adiv123").style.display = "block";
else
document.getElementById("adiv123").style.display = "none";
}
OR, based on your code: http://jsfiddle.net/Uhcxd/1/
I changed this:
<div id="adiv123" style="font:24px normal; style=display:block;">
to this
<div id="adiv123" style="font:24px normal;display:none;">
Well, for starters, your markup is invalid. Specifically, this:
style="font:24px normal; style=display:block;"
is wrong. I think you meant this:
style="font:24px normal; display:block;"
If you wanted the element hidden on page load, you actually wanted this:
style="font:24px normal; display:none;"
Related
My boss needs either an html document or a word document. That when he goes to print the document it will only show the boxes that he has checked. There is a huge list of check boxes so he does not want to print the entire thing every time. Just the ones that matter for a particular client.
I have tried several methods of javascript, and css hidden neither of which have worked. Have also tried playing with the developers macros in microsoft word. None of those have given me what I am needing.
<form id="form1" name="form1" method="post" action="">
<p>
<label>
<input type="checkbox" name="CheckboxGroup1" value="checkbox" id="box" />
Main.</label>
<br />
<label>
<input type="checkbox" value="checkbox" name="CheckboxGroup1" id="boxchecked" />
Other.</label>
<br />
</p>
</form>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script>
$(document).ready(function(){
$("#boxchecked").click(function (){
if ($("#boxchecked").prop("checked")){
$("#hidden").hide();
}else{
$("#hidden").show();
}
});
});
</script>
' Determine if there are any items checked.
If checkedListBox1.CheckedItems.Count <> 0 Then
' If so, loop through all checked items and print results.
Dim x As Integer
Dim s As String = ""
For x = 0 To checkedListBox1.CheckedItems.Count - 1
s = s & "Checked Item " & (x + 1).ToString & " = " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf
Next x
MessageBox.Show (s)
End If
The logic in your script seems to be trying to hide or show some element with the id hidden, which I don't see in your HTML anywhere.
But regardless there's a much easier way to do this with CSS.
#media print {
input[type=checkbox]:not(:checked) {
display: none;
}
}
You would use something like the media query below but you want to make sure the browser being used is compatible.
https://www.w3schools.com/cssref/sel_checked.asp
#media print {
#boxchecked:not(:checked){ display: none; }
}
or in the case of a page with many checkbox elements you can create a css class and have the media query apply to all of them:
#media print {
.cbx:not(:checked){ display: none; }
}
Hope that helps! :)
I want to do the following:
I have three checkboxes:
Hide Box1
Hide Box2
Hide Box3
I want to use Jquery to:
When Box1 checked, hide box 2 and 3, if unchecked make box 2 and 3 visible. Also where do I place the code?
Thanks in advance
Here is a complete example using the markup you gave in the comment. I also took the liberty to give the checkbox's labels which means when you click the text it will toggle the checkbox (more accessible and usable).
See on JSFiddle
HTML
<form>
<div class="toggle-checkbox">
<input type="checkbox" name="checkMeOut" id="box1" />
<label for="box1">Hide Box1</label>
</div>
<div class="toggle-checkbox">
<input type="checkbox" name="checkMeOut" id="box2" />
<label for="box2">Hide Box2</label>
</div>
<div class="toggle-checkbox">
<input type="checkbox" name="checkbox3" id="box3" />
<label for="box3">Hide Box3</label>
</div>
</form>
jQuery
$('.toggle-checkbox input[type=checkbox]').click(function () {
if ($(this).is(':checked')) {
$('.toggle-checkbox').not($(this).closest('.toggle-checkbox')).hide();
} else {
$('.toggle-checkbox').show();
}
});
To include jQuery in your page, place the following within your <head> tag.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
You could do this in between tags
$('.one').click(function () {
if ($(this).is(':checked')) {
$('input[type=checkbox]').not(this).hide();
} else {
$('input[type=checkbox]').not(this).show();
}
});
http://jsfiddle.net/davidchase03/MYASr/
Assuming your checkboxes have the ids "box1", "box2" and "box3":
$(document).ready(function(){
$("#box1").change(function(){
$("#box2, #box3").toggle();
}
}
I haven't tested this, but anytime hide box 1 is checked or unchecked, it will toggle the visibility of the other two boxes.
The optimal place for your code would be inside of a script element located just before your closing body tag, so something like
<body>
Your page stuff here
<script>
Code from above here
</script>
</body>
Here's my problem, I want an entire div to be click able, when clicked I need the radio button contained in the div to be checked, so the div acts as a radio itself. Here's what I thought I could use;
$('#content').click(function(e) {
$('input:radio[name="id_"]').prop('checked', true);
});
But this is not selecting the relative radio inside the div. I think I can use a this selector, am I right?
You don't give any code, so I guess:
DEMO
See my demo on CodePen
HTML
<div class="content">
<input type="radio" name="foo">
</div>
<div class="content">
<input type="radio" name="foo">
</div>
<div class="content">
<input type="radio" name="foo">
</div>
CSS (for example)
.content {
float: left;
width: 100px;
padding-top: 100px;
background-color: #eee;
margin-left: 10px;
}
JS (JQUERY)
$('.content').click(function(){
$(this).find('input[type=radio]').prop('checked', true);
})
Yes you can use the this selector. I have made a quick jsfiddle to show you an example.
This should do it.
$('input:radio[name*="id_"]'), assuming the name starts with id_
And yes you can use this. Use it to filter down its children like:
$(this).children('input:radio[name*=id_]').prop("checked", true)
The key is using name*=id_
This means select element whose name starts with id_. Isn't that what you wanted ?
$('#content').click(function(){
$(this).children('radio').attr('checked','checked')
})
building on Deif's solution this will toggle the checked status when clicking on the div
fiddle
<div id="content">
Some content
<input type="radio" id="radio1" value="test" />
</div>
<script type="text/javascript">
$('#content').click(function () {
var val = $(this).find('input:radio').prop('checked')?false:true;
$(this).find('input:radio').prop('checked', val);
});
</script>
Try with this:
$('div').click(function(){
if( $('div').find('input:checkbox').prop("checked") == true){
$('div').find('input:checkbox').prop("checked", false);
}
else{
$('div').find('input:checkbox').prop("checked", true);
}
});
LIVE DEMO
I am following this tutorial and designed and alert window(including online demo),
http://jquerytools.org/demos/overlay/modal-dialog.html
I could modify the source code and added radio buttons to the alert message.Source code is given bellow(you don't have to go through the whole code.Just see the place where I have added radio buttons and place where I access the value of the radio button),
<!DOCTYPE html>
<html>
<!--
This is a jQuery Tools standalone demo. Feel free to copy/paste.
http://flowplayer.org/tools/demos/
Do *not* reference CSS files and images from flowplayer.org when in
production Enjoy!
-->
<head>
<title>jQuery Tools standalone demo</title>
<!-- include the Tools -->
<script src="jquery.tools.min.js"></script>
<!-- standalone page styling (can be removed) -->
<link rel="shortcut icon" href="/media/img/favicon.ico">
<link rel="stylesheet" type="text/css"
href="/media/css/standalone.css"/>
<style>
.modal {
background-color:#fff;
display:none;
width:350px;
height:250px;
padding:15px;
text-align:left;
border:2px solid #333;
opacity:0.8;
-moz-border-radius:6px;
-webkit-border-radius:6px;
-moz-box-shadow: 0 0 50px #ccc;
-webkit-box-shadow: 0 0 50px #ccc;
}
.modal h2 {
background:url(/media/img/global/info.png) 0 50% no-repeat;
margin:0px;
padding:10px 0 10px 45px;
border-bottom:1px solid #333;
font-size:20px;
}
</style>
</head>
<body><!-- the triggers -->
<p>
<button class="modalInput" rel="#yesno">Yes or no?</button>
<button class="modalInput" rel="#prompt">User input</button>
</p>
<!-- yes/no dialog -->
<div class="modal" id="yesno">
<h2>This is a modal dialog</h2>
<p>
You can only interact with elements that are inside this dialog.
To close it click a button or use the ESC key.
</p>
<!-- yes/no buttons -->
<p>
<button class="close"> Yes </button>
<button class="close"> No </button>
</p>
</div>
<!-- user input dialog -->
<div class="modal" id="prompt">
<h2>This is a modal dialog</h2>
<p>
You can only interact.
</p>
<!-- input form. you can press enter too -->
<form>
//Added radio buttons
<input type="radio" name="sex" value="male" id="male"> Male<br />
<input type="radio" name="sex" value="female" id="female"> Female<br />
<button type="submit">Submit</button>
</form>
<br />
</div>
<script>
$(document).ready(function() {
var triggers = $(".modalInput").overlay({
// some mask tweaks suitable for modal dialogs
mask: {
color: '#ebecff',
loadSpeed: 200,
opacity: 0.9
},
closeOnClick: false
});
var buttons = $("#yesno button").click(function(e) {
// get user input
var yes = buttons.index(this) === 0;
// do something with the answer
triggers.eq(0).html("You clicked " + (yes ? "yes" : "no"));
});
$("#prompt form").submit(function(e) {
// close the overlay
triggers.eq(1).overlay().close();
// get user input
var input = $("input", this).val(); // this input value always return 'male' as the output
// do something with the answer
triggers.eq(1).html(input);
// do not submit the form
return e.preventDefault();
});
});
</script>
</body>
</html>
This demo has two alert windows. I am talking about the alert message which has the text input.In my example I have remove text input and added two radio buttons.
But when I click the 'submit' button It always return me 'male' as the output
Can anyone please help me to solve this problem? I need to get the output of the radin buttons to variable 'input'
Use this to pop up the box. In the HTML keep the radio button.
<table style="width: 100%; border: 0px;" cellpadding="3" cellspacing="0">
<tr>
<td class="web_dialog_title">Email this Article</td>
<td class="web_dialog_title align_right">
Close
</td>
</tr>
<TR><TD>
Use radio button.
</TD></TR>
</table>
<script type="text/javascript">
$(document).ready(function ()
{
$("#btnShowSimple").click(function (e)
{
ShowDialog(false);
e.preventDefault();
});
$("#btnClose").click(function (e)
{
HideDialog();
e.preventDefault();
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
HideDialog(); }
});
});
function ShowDialog(modal)
{
$("#overlay").show();
$("#dialog").fadeIn(300);
if (modal)
{
$("#overlay").unbind("click");
}
else
{
$("#overlay").click(function (e)
{
HideDialog();
});
}
}
function HideDialog()
{
$("#overlay").hide();
$("#dialog").fadeOut(300);
}
you changed text type input with radio type input . so you must change
var input = $("input", this).val();
with this one
var input = $("input:checked", this).val();
perdickss provides a very good answer for this example.
But that will not work if you have multiple inputs in the page. So you should use something like this:
var input = $("input[name=FIELD_NAME]:checked", this).val();
Where FIELD_NAME is the name you have for the inputs you want to check. In this case the code would look like:
var input = $("input[name=sex]:checked", this).val();
As you wrote in your code:
<form>
<input type="radio" name="sex" value="male" id="male"> Male<br />
<input type="radio" name="sex" value="female" id="female"> Female<br />
<button type="submit">Submit</button>
</form>
There are two input fields in your #prompt form, so when you get value with this code:
var input = $("input", this).val();
You will always get value of first matched element (which is male).
All you have to do is specify checked input using :checked selector, I suggest you additionally to add input's name:
var input = $("input[name=sex]:checked", this).val();
I printed to the screen 16 icons (little pictures).
Now I want to be able to select icons,
and when I press a button the selected icons ids will be sent in a form.
I saw in the net only checkboxes and lists multiselect,
what's the best way to do this?
(I'm pretty new to web design)
thanks ahead!
Although jQuery isn't in your tags, you should introduce yourself to jQuery. It'll make your life easier, for what you're trying to do. Here is the basic steps both if you use jQuery and if use just Javascript:
With jQuery
Give all your icons a class and each one a unique id:
<img src='icon1.png' data-iconID=2233 class='myIcons' />).
Then bind that class to a click event
$('.myIcons').bind('click', function() {
$(this).toggleClass('selectIcon');
});
Attach form submit function to onsubmit:
<form ... onsubmit="submitForm();">
Build submitForm function:
function submitForm() {
var csvIconIds = '';
$.each($('.myIcons.selectIcon'), function (index, value) {
csvIconIds += $(value).attr('data-iconID');
});
//submit scvIconIds here along with other form data (ajax?)
}
With Javascript
Similar as above but way more complicated...
To toggle classes see this thread: How to add/remove a class in JavaScript?
To getting attributes by class see this site: http://www.actiononline.biz/web/code/how-to-getelementsbyclass-in-javascript-the-code/
This could be a way using just plain Javascript or jQuery. I prefer the jQuery version, since it separates the click handler from the markup, instead of using inline onclick handlers, which are in general discouraged.
What this does is use an input element array, which you can create by adding [] to the element name. This same technique can be used on SELECTs and other elements, since it signals to the server that an array has been submitted, as opposed to value known by a single key.
<html>
<head>
<style type="text/css">
div img {
cursor: pointer;
border: 1px solid #f00;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<script>
function setFormImage(id) {
if (id != '' && !document.getElementById('input_'+id)) {
var img = document.createElement('input');
img.type = 'text';
img.id = 'input_'+id;
img.name = 'images[]';
img.value = id;
document.imageSubmit.appendChild(img);
}
}
$(document).ready(function(){
$('#jqueryimages img').click(function(){
setFormImage(this.id);
});
});
</script>
</head>
<body>
<pre><?php
if (count($_GET['images'])) {
print_r($_GET['images']);
}
?></pre>
<div style="float: left; width: 49%;">
<h1>Plain ol' HTML</h1>
1. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-1" onclick="setFormImage(this.id)"/>
<br/>
2. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-2" onclick="setFormImage(this.id)"/>
<br/>
3. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-3" onclick="setFormImage(this.id)"/>
<br/>
4. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-4" onclick="setFormImage(this.id)"/>
</div>
<div id="jqueryimages" style="float: left; width: 49%;">
<h1>jQuery</h1>
5. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-5"/>
<br/>
6. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-6"/>
<br/>
7. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-7"/>
<br/>
8. <img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" id="img-8"/>
</div>
<h1>Form Submit</h1>
<form name="imageSubmit" method="get">
<input type="submit" value="View Selected"/>
</form>
</body>
</html>
try this
var idArray = [];
$("#container-id img").each(function(index,value){
idArray.push($(value).attr("id"));
});
//do anything with the array