<form action="#" onsubmit="return validateForm()" method="post">
<table>
<tr>
<td><p> 1. </p></td>
<td><label> I am cool </label></td>
<div class="allQuestion">
<?php for($i=1; $i<=10; $i++){?><td><input type="radio" name="Dquestion[1]" value="<?=$i?>"> <?=$i?> </td> <?php } ?>
</div>
</tr>
</table><!-- strength_table end -->
<input type="submit" value="Submit"><br/>
</form>
js
function validateForm(){
var questions = document.getElementsByClassName("allQuestion");
for( var j=0; j<questions.length; j++){
if( !isOneInputChecked(questions[j], "radio")){
formValid = false;
}
}
alert(formValid ? "Submisson succesful!" : "Submisson Failed");
return formValid;
}
function isOneInputChecked(sel){
var inputs = sel.getElementsByTagName('input');
for(var k = 0; k < inputs.length; k++){
if(inputs[k].checked)
return true;
};
return false;
};
i am using this to validate my radio question , it work perfectly but once i put the <td> before <input> , it work weird , i know it target the all input warp by allQuestion and td is blocking it.
any idea how i keep the td in place and make the script works ?
i tryed this code but didt work
var questions = document.getElementsByClassName("allQuestion").getElementsByTag("Td");
Avoid using table elements as a layout. I know it seems convenient when you first start off, but save yourself the trouble and just go straight into learning to style with css
function validateForm(){
var checked = document.querySelector('form').elements.some(function(el){
return el.checked;
});
if(checked){
//at least one is checked
} else {
//none are checked
}
}
I didn't exactly test this, but just try to give an id to the td that u are targeted.
var questions = document.getElementsByTag("id_name"); //id_name is the id of the td
*just extra notes, using table is not a good practice, use other simple HTML and CSS for more flexibilty and readble code.
Related
I have a form which is disabled using <fieldset disabled="disabled">. Now I want to enable the same form. I've used javascript but it isn't enabling my form. Here is my form and javascript code:
<?php echo "<input type='button' id='disable_enable_button' onclick='enableForm()' value='Enable/Disable'>"; ?>
<form id="form1>
<fieldset disabled="disabled">
<table>
<thead>
<tr>Sr no</tr>
<tr>Application</tr>
<tr>Comments</tr>
</thead>
<tbody>
<tr>1</tr>
<tr>ABC</tr>
<tr><textarea></textarea></tr>
</tbody>
</table>
</fieldset>
</form>
function enableForm() {
var form = document.getElementById("form1");
var elements = form.elements;
for (var i = 0, len = elements.length; i < len; ++i) {
elements[i].readOnly = "";
}
}
I see a few potential issues here. I don't know how different your code here is than your actual file, but make sure that your JavaScript code is wrapped with <script></script> tags. In addition to this, I never see a call to the function enableForm(), (again, could be different in your actual code), although I see the function showForm() call a few times. Make sure this function is called, probably upon some user action (ie button click).
Feel free to comment on this with any issues/corrections, and I will help resolve them!
I´m trying to create a function to change input value in the HTML script. HTML looks like this:
<tr>
<td>6500</td>
<td>
<input type="text" size="3" value="3"/>
</td>
</tr>
<tr>
<td>6500</td>
<td>
<input type="text" size="3" value="3"/>
</td>
</tr>
I want to create an event where the value updates depending on the userinput and change the value so that when I click the button it performs the calculation which Ive already written functions for. I am getting stuck, only thing I have is the cells where userinput is an option. I need to do simle plain js and not jquery.
function input() {
var table = document.getElementById("list");
var x = table.getElementsByTagName("input");
console.log(x);
}
Thanks in advance.
Not sure why I never replied with an answer after your follow up comment, and I know this is terribly late, but if this no longer helps you, it may still help others.
Using JavaScript:
var inputs = document.querySelectorAll('input');
for (var i = 0; i < inputs.length; i++) {
inputs[i].addEventListener('input', function() {
this.setAttribute('value', this.value);
});
}
Using jQuery:
$(document).on('input', 'input', function() {this.setAttribute('value', this.value);});
I've been searching for this for a couple hours with no luck. This seems like it should be fairly easy but I am obviously overlooking something.
I have a table, with each row displaying information in each cell. At the end of each of the rows, there is an additional cell with a checkbox in it. The checkbox is an array, and each checkbox value is an imploded array via PHP. See below:
HTML/PHP
--------
(...some html code...)
<form method="post" action="the-next-page.php">
<table>
<tr>
<?php
(...some php SQL query code...)
while ($row = oci_fetch_array($result)) {
?>
<td><input type="text">Name</td>
<td><input type="text">City</td>
<td><input type="checkbox" name="checkGroup[]" value="<?php implode(":",$someArrayvariable) ?>"></td>
<?php
}
?>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
....
</html>
Passing the imploded values to the next page works fine. No problem there.
I have been trying to create a javascript function to check all of the boxes that are in this form, or under the checkbox group's name, or whatever I can do to check them all with the click of a button. I've tried variations of the following with no success:
HTML (On the top of the same script as above)
----
<button name="checkAll" onclick="checkAll()">Check All</button>
Javascript (On the bottom of the same script as above)
----
<script type="text/javascript">
function checkAll() {
var checks = document.getElementByName("checkGroup");
for (var i=0; i < checks.length; i++) {
checks[i].checked = true;
}
}
</script>
I can't figure out what I'm doing wrong. I know that a variation of this question has been asked before many times, but I don't seem to be getting any results. I'm guessing because my checkboxes name is an array (checkGroup[] ???).
When I click the button to check all of the checkboxes in the form, nothing happens.
Any ideas? Thanks in advance.
-Anthony
You can use JQuery to make this easier on yourself.
I would also assign a general class name to each checkbox input, so then in Javascript (using JQuery):
$(".classname").each(function() {
$(this).prop("checked",true);
});
I would also give the Check All button a unique class/id so you can do this
$(document).ready(function() {
$("#allcheckboxid").on("click",function() {
$(".classname").each(function() {
$(this).prop("checked",true);
});
});
})
Two minor things:
function checkAll() {
var checks = document.getElementsByName("checkGroup[]");
for (var i=0; i < checks.length; i++) {
checks[i].checked = true;
}
}
getElementByName should be getElementsByName, and checkGroup should be checkGroup[]. Other than that your code should be good to go!
Try this way to get all checked check box elements
<button name="checkAll" onclick="checkAll()">Check All</button>
<script type="text/javascript">
function checkAll() {
var checkboxes = document.getElementsByName('checkGroup[]');
var checkboxesChecked = [];
for (var i=0; i<checkboxes.length; i++)
{
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i]);
}
}
return checkboxesChecked.length > 0 ? checkboxesChecked : null;
}
</script>
Please help me out on this. I have Javascript like the following:
function calc() {
var table = document.getElementById(tableNum);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var totalNum[i] = document.formNum.txt1[i].value * document.formNum.txt2[i].value;
document.getElementById('totalCalc[' + i + ']').innerHTML = totalNum;
}
}
And HTML like this:
<table id="tableNum">
<form name="formNum" action="" id="formNum">
<tr>
<td><input type="text" name="txt1[]" onkeyup="calc()"/></td>
<td><input type="text" name="txt2[]" onkeyup="calc()"/></td>
<td><span id="totalCalc[]"></span></td>
</tr>
</form>
</table>
The number of input fields is unknown. No error, but totalCalc field is empty. Please tell me what I have done wrong. Thanks.
EDIT: I'm sorry, I forgot to mention both the input fields are in a table. Please check the edited code. Thanks.
EDIT: I'm actually working on a demo which the number of table row is defined by user, by clicking insert row button.
EDIT: Thanks Travis for the code. After a few changes, the code is working now. But only the first row is working. I'm thinking to get the length of the row and to use for loop for the text fields. <input type="text" name="txt1[<?php echo $rowLength;?>]" onkeyup="calc()"/> Does anyone have other ideas? Thanks.
The first thing seems wrong is
document.getElementById(tableNum);
should be
document.getElementById("tableNum");
Secondly,
var totalNum[i] =
should be
var totalNum =
Also, its not working, you can find it out quickly by debugging through firebug or chrome's integrated developer tool. Which will help you for syntax verification as well.
Here is what is going on.
HTML first
If you are going to reference these by indices, then use proper indices, like this
name="txt1[0]"
name="txt2[0]"
<span id="totalCalc[0]">
Javascript
document.getElementById(tableNum);
getElementsById expects a string, so this should be
document.getElementById("tableNum");
Since you are iterating, you only need one of these variables since it is immediately used (not a whole array):
var totalNum = instead of var totalNum[i]
When you access the form using dot notation, the brackets in the name messes that up, you need to do it like this:
document.formNum["txt1["+i+"]"].value instead of document.formNum.txt1[i].value
Vuala
When you make these minor changes, the code you used will actually produce proper results :) See this working demo: http://jsfiddle.net/69Kj7/ , also, here is a demo with 2 rows: http://jsfiddle.net/69Kj7/1/
For reference, this is the code in the demo:
html:
<table id="tableNum">
<form name="formNum" action="" id="formNum">
<tr>
<td><input type="text" name="txt1[0]" onkeyup="calc()"/></td>
<td><input type="text" name="txt2[0]" onkeyup="calc()"/></td>
<td><span id="totalCalc[0]"></span></td>
</tr>
</form>
</table>
js:
function calc() {
var table = document.getElementById("tableNum");
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var totalNum = document.formNum["txt1["+i+"]"].value * document.formNum["txt2["+i+"]"].value;
document.getElementById('totalCalc[' + i + ']').innerHTML = totalNum;
}
}
if you wants to work with pure java script and here is the logical code
html
<form name="formNum" id="formNum" action="" >
<input type="text" name="foo[]" onkeyup="calc()" value="5"/>
<input type="text" name="foo[]" onkeyup="calc()" value="12"/>
<span id="totalCalc"></span>
</form>
js
var inputs = formNum["foo[]"];
var total = 1;
alert(inputs.length);
for (var i = 0; i < inputs.length; i++) {
total *= inputs[i].value;
}
alert(total);
working DEMO
I've figured out how to solve the problem. Just insert array after totalCalc, but not within totalCalc.
Thank you guys so much for helping me out :)
I have a poll with a couple a questions. Here is html code
<form id="pool">
<div class="questions>
<input type="radio" name="sex">Male
<input type="radio" name="sex">Female
</div>
<div class="questions>
<input type="radio" name="hair">Brown
<input type="radio" name="hair">Blonde
</div>
.... a lot of qestions div's
</form>
What to do so after the form is submitted to be sure that there is a checked radio button in all div`s ?
If you know how many groups you have you can just do:
if($('#pool input:radio:checked').length < numGroups){
// At least one group isn't checked
}
Otherwise you need to count the number of groups first. I can't think of any way to do this better then:
var rgroups = [];
$('#pool input:radio').each(function(index, el){
var i;
for(i = 0; i < rgroups.length; i++)
if(rgroups[i] == $(el).attr('name'))
return true;
rgroups.push($(el).attr('name'));
}
);
rgroups = rgroups.length;
if($('#pool input:radio:checked').length < rgroups)
alert('You must fill in all the fields.');
else
alert('Thanks!');
set default values or create handler for submit button and check if some values was checked. If no radio button is checked, show error message and do not submit form ( return false)
Untested code, but this is the idea:
if($('#pool').children().length == $('pool
div').find('#pool input:radio:selected').length) {
//do stuff
}
You can use the jquery validate plugin
from my experience this plugin is very efficient