I have a problem with this code :
jQuery(".cb-disable").click(function(){
var parent = jQuery(this).parents('.switch');
var vid =this.id;
jQuery('#'+vid).attr('id','');
jQuery('#'+vid).attr('id','');
jQuery('.cb-enable',parent).removeClass('selected');
jQuery(this).addClass('selected');
jQuery('input[name='+vid+'-off]').attr('checked', true)
jQuery('input[name='+vid+'-on]').removeAttr('checked');
jQuery('input[name='+vid+']').attr('name', vid+"-on");
jQuery('input[name='+vid+'-off]').attr('name', vid);
});
});
because this code is correct for the example code below.
<p class="wpptabs_hoverable-286 switch">
<input type="radio" id="ON" class="on286" name="wpptabs_hoverable-286" value="on" checked="checked">
<input type="radio" id="OFF" class="off286" name="wpptabs_hoverable-286-off" value="off">
<label for="ON" id="" class="cb-enable"><span>ON</span></label>
<label for="OFF" id="" class="cb-disable selected"><span>OFF</span></label>
</p>
But it isn't correct. Can you help me?
This code is based on http://devgrow.com/iphone-style-switches/
for php generating a checkbox in table column.
I have question: "What is wrong in this javascript code?".
Sorry for my english, but I rarely write in this language
Your names have to be the same for each radio input, otherwise they act independently (meaning they can show both on and off state) — in the code below I have deleted the -off part in the name for the off radio:
<p class="wpptabs_hoverable-286 switch">
<input type="radio" id="ON" class="on286" name="wpptabs_hoverable-286" value="on" checked="checked">
<input type="radio" id="OFF" class="off286" name="wpptabs_hoverable-286" value="off">
<label for="ON" id="" class="cb-enable"><span>ON</span></label>
<label for="OFF" id="" class="cb-disable selected"><span>OFF</span></label>
</p>
You can see that the on and off buttons now work as expected here:
http://jsfiddle.net/S8qFT/
update
It seems that getting the radios to work wasn't your only issue. I've simplified the javascript so it only does what it needs to, it should work as you required now.
http://jsfiddle.net/65JRx/
javscript
/// make sure we apply the click handling to both on and off labels
$(".cb-enable,.cb-disable").click(function(){
/// find the elements we need
var self = $(this);
var parent = self.parents('.switch');
/// handle the selected highlight, first remove all selected
parent.find('.selected').removeClass('selected');
/// then select the right one again
self.addClass('selected');
/// most modern browsers should handle transfering the label click
/// to the actual radio (via `for` and `id`), but just in case.
if ( self.is('.cb-enable') ) {
parent.find('input[value=on]').prop('checked', true);
}
else {
parent.find('input[value=off]').prop('checked', true);
}
});
markup
<div class="wpptabs_hoverable-286 switch">
<!-- I've laid these inputs out just to make them
easier to read, I wouldn't normally lay markup out
this way //-->
<input
type="radio"
class="on286"
name="wpptabs_hoverable-286"
id="wpptabs_hoverable-286-on"
value="on"
checked="checked"
/>
<!-- I've renamed your IDs to have more specific names
this will mean you can use this method on more than
one set of inputs -- as long as you keep the IDs inline
with your id convention. //-->
<input
type="radio"
class="off286"
name="wpptabs_hoverable-286"
id="wpptabs_hoverable-286-off"
value="off"
/>
<label
for="wpptabs_hoverable-286-on"
class="cb-enable selected">
<span>ON</span>
</label>
<label
for="wpptabs_hoverable-286-off"
class="cb-disable">
<span>OFF</span>
</label>
</div>
css
/**
* Added a highlight so you can see selected working
*/
label.selected { color: blue; }
/**
* I've removed the display none so you can still see the inputs
* working as they should. Obviously you can put the display none
* back to get your working version.
*/
.switch input{/*display: none;*/ opacity: 0.5; position: relative;}
Related
I have an autocalc running on a form and the total is shown through a div ID. I need to be able to somehow render the div ID answer into a php form mailer.
I’ve tried adding a hidden input field with the div ID.
<div id="totalPriceTM">
<input name="Score" id="totalPriceTM" value="" type="hidden" />
</div>
No error messages but result not showing in email.
You should create different div name on your form.
for example :
<script>
function autocalc(x,y)
{
a = x*y;
document.getElementById(totalPriceTM).innerHTML = a;
document.getElementById(totalPriceTM2).value = a;
}
</script>
<div id="totalPriceTM">
<input name="Score" id="totalPriceTM2" value="" type="hidden" />
</div>
result will be show on div totalPriceTM and will be set the value of div totalPriceTM2
I'm working on a PHP self-evaluation form that has 5 question categories with 10 questions each. In the beginning of the application, I have 5 checkboxes to represent these categories, and they are automatically checked. The idea is that whenever user unchecks a category, the questions of that category instantly disappear from the form, and when they check it again, they come back. Something that should be achievable with the help of jQuery and AJAX.
I made the checkboxes with CodeIgniter's form_helper:
for($i = 1; $i<=5; $i++) {
$this->formapp_model->printCatName($i);
$data = array('name'=>'category$i', 'id'=>'category$i', 'value'=>'$i', 'checked'=>TRUE);
echo form_checkbox($data);
}
And I have a function to printing all 10 questions of the category from database after their category id, which works fine when I just post them as they are:
$this->formapp_model->printCategory(1);
$this->formapp_model->printCategory(2);
$this->formapp_model->printCategory(3);
$this->formapp_model->printCategory(4);
$this->formapp_model->printCategory(5);
Using the help of this:
Passing whether a checkbox is checked, via jQuery, to PHP
I was able to gather that for the jQuery, I need something like
var category1 = $('#category1:checked').val();
in order to check if the checkbox has been selected. I also tried
var category1 = $('#category1:checked').post();
as it seemed logical to use post in order to PHP to recognize it.
And for the print selection something like
if (isset($_POST['category 1'])) { $this->formapp_model->printCategory(1); } else { echo "This category is not selected."; }
I tried this, but PHP doesn't recognize the message that jQuery is giving it, meaning the category's questions disappeared permanently, whether the checkbox was checked or not. I checked with echo var_export($_POST); and noticed that all the jQuery is printing out is: array ( ). The question mentioned above was very informative, but missed some info that I would have needed to get it to work. The asker was also using an array instead of separate variables so I don't know how to edit it properly.
I'm a complete newbie with jQuery and AJAX so I have a hard time grasping what I need in order to get jQuery and PHP communicate dynamically the way I described. I have run around stackoverflow to find similar cases, but none of them have quite had what I need. However, I deeply apologize in case this is a repeativive question. Thank you to anyone who helps!
PHP is server side, you need the questions to appear/disappear client side meaning you want to make that happen using jQuery itself (or regular js but since you're already loading jQuery it's quicker just to use the library itself).
To be honest I'm not really following how your view is working so I'll just give some basic code to give you the idea. You create the checkboxes and the questions giving each a unique ID. Then in the on click method for the check boxes you determine which questions to show.
HTML:
<div id="checkboxes">
<input type="checkbox" id="box1" class="check" checked="checked"/>
<input type="checkbox" id="box2" class="check" checked="checked" />
<input type="checkbox" id="box3" class="check" checked="checked" />
<input type="checkbox" id="box4" class="check" checked="checked" />
<input type="checkbox" id="box5" class="check" checked="checked" />
</div>
<div id="question1" class="question">
<p>Question 1</p>
</div>
<div id="question2" class="question">
<p>Question 2</p>
</div>
<div id="question3" class="question">
<p>Question 3</p>
</div>
<div id="question4" class="question">
<p>Question 4</p>
</div>
<div id="question5" class="question">
<p>Question 5</p>
</div>
jQuery:
$(".check").on("click",function(){
var id = $(this).attr("id");
var id2 = id.substr(id.length -1);
var question = "question"+id2;
if($(this).is(":checked"))
{
$("#"+question).css("display","block");
} else {
$("#"+question).css("display","none");
}
});
Demo: http://jsfiddle.net/calder12/taSPX/1
Simple php/mysql/js project required me to let user be able to customize some forms.
so i wrote this page so user can edit default forms, add his own inputs and drag them around to arrange them as he want.
now my question is how can i save the end result ? should i input the style of each input into mysql ? or should i extract all css of all inputs and save them as text into mysql ?
any ideas ?
Fiddle example
here is my code
html
<h1>Form editer</h1>
<div id='form' style="position:relative;width:100%;border:1px dashed grey"></div>
<div id="generator">
<label for="label">label</label><input type="text" value="" name="label">
<label for="system">System name</label><input type="text" value="" name="system">
<label for="type">input type</label><select name="type">
<option value="input">input</option><option value="dropdown">dropdown</option</select>
<label for="value">value</label><input type="text" value="" name="value">
<button type="button" name="generate">generate</button>
<div style="border:1px solid;width:250px;height:50px;margin: 0 auto;" id="tempo"></div>
<button id="add_2_form" type="button" name="add to form">add to form</button>
</div>
Js (plugins : jquery,draggable)
$('button[name="generate"]').click(function(){
var html,
label=$('#generator input[name=label]').val(),
sys=$('#generator input[name=system]').val(),
type=$('#generator [name=type]').val(),
value=$('#generator input[name=value]').val(),x;
if(type == 'dropdown'){
html='<label for="'+sys+'">'+label+'</label><select name="'+sys+'">';
arr=value.split(",");
for (x in arr)
{
html=html+'<option value="'+arr[x]+'">'+arr[x]+'</option>';
}
html=html+'</select>';
$('#tempo').html(html);
}else{
html='<label for="'+sys+'">'+label+'</label><input type="'+type+'" name="'+sys+'" value="'+value+'">';
$('#tempo').html(html);
}
});
$('#add_2_form').click(function(){
var html='<p>'+$('#tempo').html()+'</p>';
$('#form').append(html);
$('#form p').css('border','1px dashed blue').draggable();
});
this is my code so far, but now how can i save the form after user create it and draged around all inputs?
You have to build your own positioning mechanism and save them in a session or cookies (with PHP) or local storage (with JavaScript). In the second time user visit this page, check the existence of the old settings and restore the UI by setting the position to suit with what you saved.
I think the most simple mechanism is store the element name (or id) and its top, left, ... values.
I'm working on a page redesign, and I've added 6 different buttons for the 6 options a user can choose instead of 6 radio boxes & 1 button. I just need to adjust the code to make sure that on the next page, the right total and text is displayed based on what button is pushed.
I just need some of the text on the next page to change depending on which of the buttons is pressed.
Here is what was there originally:
<input id="diamondmonthly" class="radio" type="radio" value="diamondmonthly" name="membership">
<input id="diamondyearly" class="radio" type="radio" checked="checked" value="diamondyearly" name="membership">
<input id="c4" class="radio" type="radio" value="c4" name="membership">
<input id="c3" class="radio" type="radio" value="c3" name="membership">
<input id="c2" class="radio" type="radio" value="c2" name="membership">
<input id="c1" class="radio" type="radio" value="c1" name="membership">
<a id="btnSave" class="xlarge button-submit" onclick="$j('#btnSave').attr('disabled', 'disabled');qc.recordControlModification('btnSave', 'Enabled', '0'); qc.pB('MembershipForm', 'btnSave', 'QClickEvent', '');" href="#">
<script>
//<![CDATA[
qc.registerForm(); qc.imageAssets = "http://images.comfiles.com/assets/images"; qc.cssAssets = "http://images.comfiles.com/assets/css"; qc.phpAssets = "/assets/php"; qc.jsAssets = "http://images.comfiles.com/assets/js"; qc.regCA(new Array("btnSave","btnSave2","c1","c2","c3","c4","diamondyearly","diamondmonthly")); jQuery(document).ready(function($j){if($j.isFunction($j().tooltip)){$j("#MembershipForm :input[data-error]").tooltip({ position: "center right", tipClass: "error_tooltip", offset: [0, -4] })}});
//]]>
</script>
This can be dealt with using a listener one each button, or by delegating the work to a single ancestor element. In this case, it's probably easiest to put a single click listener on the form. When it's called, you can use the associated event object to see where it came from and react accordingly.
Note that when getting form controls by name, if only a single control has that name then only a single element is returned. If two or more controls have the name, then a HTMLCollection of the elements with that name is returned.
The listener can be something like:
<form onclick="handleClick(this)" ...>
And the listener (untested but you should be able to get it going from here):
function handleClick(form) {
var button, buttons = form['membership'];
// Now show text depending on which membership button is checked
for (var i=0, iLen=buttons.length, i<iLen; i++) {
button = buttons[i];
// Assuming that the text element to display has an ID
// matching the value of the checked button
if (button.checked) {
document.getElementById(button.value).style.display = '';
} else {
document.getElementById(button.value).style.display = 'none';
}
}
}
The script will run every time there is a click anywhere in the form, so the right text will always be displayed, even if the reset button is clicked.
The if..else can be replaced by:
document.getElementById(button.value).style.display = button.checked? '' : 'none';
but the longer version may be clearer.
Oh, and get rid of the CDATA delimiters in the script element, they are only required for documents that are served as XML.
I toggle a div using something like this -
<input type="radio" name="myRadio" value="myDiv_1" />MyDiv
<input type="radio" name="myRadio" value="myDiv_2" />MyDiv2
<input type="radio" name="myRadio" value="myDiv_3" />MyDiv3
<div id="myDiv_1"> 1 Some input fields, text </div>
<div id="myDiv_2"> 2 More input fields, text </div>
<div id="myDiv_3"> 3 More input fields, text </div>
JAVASCRIPT
$('#myDiv_1').hide();
$('#myDiv_2').hide();
$('#myDiv_3').hide();
$('input[name="myRadio"]').change(function() {
var selected_type = $(this).val();
switch(selected_type) {
case "myDiv_1":
$('#myDiv_1').slideDown();
//if others are visible just slideup
$('#myDiv_2').slideUp();
$('#myDiv_3').slideUp();
break;
case "myDiv_2":
$('#myDiv_2').slideDown();
//if others are visible just slideup
$('#myDiv_1').slideUp();
$('#myDiv_3').slideUp();
break;
case "myDiv_3":
$('#myDiv_3').slideDown();
//if others are visible just slideup
$('#myDiv_2').slideUp();
$('#myDiv_1').slideUp();
break;
}
}
);
This works fine. My question is how I can improve it and make it more flexible as if I have to MORE divs I have to modify all cases of switch.
Also should enclose the switch functionality in a function and bind this function to events such as click and change (just to ensure that toggling works)??
Thanks for your help.
This works, I just tested it.
<script type="text/javascript">
$(document).ready(function(){
$('.MyDiv').hide();
$('input[name="myRadio"]').change(function(){
var selected = $(this).val();
$('.MyDiv').slideUp();
$('#'+selected).slideDown();
});
});
</script>
The radio buttons should look like this, where the value is the id of the element that should be shown.
<form action="example.com" method="post">
<input type="radio" name="myRadio" value="myDiv_1" />MyDiv<br />
<input type="radio" name="myRadio" value="myDiv_2" />MyDiv2<br />
<input type="radio" name="myRadio" value="myDiv_3" />MyDiv3<br />
<input type="radio" name="myRadio" value="myDiv_4" />MyDiv4
</form>
And finally, the divs should look like this, all having the class MyDiv:
<div id="myDiv_1" class="MyDiv">Div number 1!</div>
<div id="myDiv_2" class="MyDiv">Div number 2!</div>
<div id="myDiv_3" class="MyDiv">Div number 3!</div>
<div id="myDiv_4" class="myDiv">Div number 4!</div>
The following is based on the code you pasted here - before using, read below:
$("div").hide();
$("input[name='myRadio']").change(function(){
$("div:not(#"+$(this).val()+")").slideUp();
$("div#"+$(this).val()).slideDown();
});
Before Using...
I would suggest you add a class to each of the collapsable panels, maybe .panel. And then update the selectors to modify only div.panel instead of every div on the page.
your solution doesn't work in IE 8-- actually has the opposite behavior. Use the "click" function instead of "change"
$('.myDiv').hide();
$('input[name="myRadio"]').click(function(){
var selected = $(this).val();
$('.myDiv').slideUp();
$('#'+selected).slideDown();
});