
|
Reviewers required : jQuery 2.0 Development Cookbook - free e-copy will be provided Packt is looking for people interested in reviewing the book jQuery 2.0 Development Cookbook - on their blog/ amazon. If interested, please let me know at – [email protected] or leave a comment The book is written by Leon Revill Know more - http://www.packtpub.com/jquery-2-development-cookbook/book Note : Limited copies available. Thanks and regards
|
|
|
|

|
<pre lang="css">Reviewers required : jQuery 2.0 Development Cookbook - free e-copy will be provided Packt is looking for people interested in reviewing the book jQuery 2.0 Development Cookbook - on their blog/ amazon. If interested, please let me know at – [email protected] or leave a comment The book is written by Leon Revill Know more - http://www.packtpub.com/jquery-2-development-cookbook/book Note : Limited copies available. Thanks and regards</pre>
|
|
|
|

|
I have been trying to get the <body onunload...> method to work. I have seen and tried quite a few online examples including the example found on W3C (which is below). Even on their site, it doesn't work. I have tried to get the method to work by:
Refresh the page, using back and forward buttons, and closing the window. Nothing happens in IE, Firefox or Chrome. I am at a loss as to why this doesn't work.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Thank you for visiting W3Schools!");
}
</script>
</head>
<body onunload="myFunction()">
<h1>Welcome to my Home Page</h1>
<p>Close this window or press F5 to reload the page.</p>
</body>
</html>
|
|
|
|

|
I need to check whether the radio button is selected or not. So, when I click on submit button, it will check the radio button, if there's unselected button then show alert, if button is selected then move to another page. Can anyone help me using Javascript??? Thank you
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form id="form" name="form" action="" method="post">
<style type="text/css">
.tftable {font-size:16px;color:#333333;width:50%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}
.tftable th {font-size:24px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}
.tftable tr {background-color:#d4e3e5;}
.tftable td {font-size:16px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}
.tftable tr:hover {background-color:#ffffff;}
</style>
<table class="tftable" border="1">
<tr><th width="400"><div align="center" class="style2">Questions</div></th><th colspan="2"><div align="center" class="style2">Answer</div></th>
</tr>
<tr><td><span class="style1">I like to listen to songs on the radio or a CD</span></td>
<td width="70"><span class="style1">
<label></label>
<label>
<input name="bm1" type="radio" value="1" />
Yes</label>
</span></td>
<td width="72"><span class="style1">
<label></label>
<label>
<input name="bm1" type="radio" value="2" />
No
</label>
</span></td>
</tr>
<tr><td><span class="style1">I like to watch music videos on TV</span></td><td><span class="style1">
<label>
<input name="bm2" type="radio" value="1" />
Yes</label>
</span></td>
<td><span class="style1">
<label>
<input name="bm2" type="radio" value="2" />
No </label>
</span></td>
</tr>
<tr><td><span class="style1">I like to go to music concerts and hear live music</span></td><td><span class="style1">
<label>
<input name="bm3" type="radio" value="1" />
Yes</label>
</span></td>
<td><span class="style1">
<label>
<input name="bm3" type="radio" value="2" />
No </label>
</span></td>
</tr>
<tr><td><span class="style1">I can easily remeber tunes, raps, or melodies</span></td><td><span class="style1">
<label>
<input name="bm4" type="radio" value="1" />
Yes</label>
</span></td>
<td><span class="style1">
<label>
<input name="bm4" type="radio" value="2" />
No </label>
</span></td>
</tr>
<tr><td><span class="style1">I take music lessons, singing lessons, or play a muscial instrument</span></td><td><span class="style1">
<label>
<input name="bm5" type="radio" value="1" />
Yes</label>
</span></td>
<td><span class="style1">
<label>
<input name="bm5" type="radio" value="2" />
No </label>
</span></td>
</tr>
<tr><td><span class="style1">I can learn new songs easily</span></td><td><span class="style1">
<label>
<input name="bm6" type="radio" value="1" />
Yes</label>
</span></td>
<td><span class="style1">
<label>
<input name="bm6" type="radio" value="2" />
No </label>
</span></td>
<tr><td><span class="style1">I like to sing</span></td><td><span class="style1">
<label>
<input name="bm7" type="radio" value="1" />
Yes</label>
</span></td>
<td><span class="style1">
<label>
<input name="bm7" type="radio" value="2" />
No </label>
</span></td>
</tr>
</table>
</form>
</body>
</html>
|
|
|
|

|
First, add a javascript code in the header to validate that all radios are checked, if all checked, proceed to submit else cancel the submit. Next you have to add a submit button in the form, last, modify the form tag to add an onsubmit event to call the validation javascript code. See example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function validateRadio()
{
var inputs = myform.elements;
var radios = [];
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type == 'radio') {
radios.push(inputs[i]);
}
}
var countChecked = 0;
for (var j = 0; j < radios.length; j++) {
if (radios[j].checked) {
countChecked++;
}
}
if (countChecked != radios.length / 2){
alert("All questions must be answered.");
return false; } else {
return true; }
}
</script>
</head>
</head>
<body>
<form name="myform" method="post"
onSubmit="return validateRadio()" action="anotherfile.php">
<!-- your other code --->
</table>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>
|
|
|
|

|
Thanks Peter Leow...
It's works for me!!!
GBU...
|
|
|
|

|
I add another code for counting button with value=1
I keep getting the result = 0
Can you tell me what do i miss?
Here's the code
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type == 'radio') {
radios.push(inputs[i]);
}
}
var countChecked = 0;
for (var j = 0; j < radios.length; j++) {
if (radios[j].checked) {
countChecked++;
}
var answeramount = 0;
if(radios[j].checked && radios[j].value==1){
answeramount++;
}
}
I get it fix by making new loop. But i still confused why the first code didn't work...
Here's the new code
var countChecked = 0;
for (var j = 0; j < radios.length; j++) {
if (radios[j].checked) {
countChecked++;
}
}
var answeramount = 0;
for (var k = 0; k < radios.length; k++){
if(radios[k].checked && radios[k].value==1){
answeramount++;
}
}
|
|
|
|

|
That is because you have placed this line of code inside the for loop which keep resetting it to zero.
var answeramount = 0;
If your problem has been answered then edit your message and add [Solved] to the subject line of your original post, and cast an approval vote to the one or several answers that really helped you." Thanks.
|
|
|
|

|
Hi,
I know that objects in javascript, like in many other languages, holds the reference to the object itself and primitive types like number bool holds the actual value.
I have something like this:
function manager()
{
var anObject = {prop1: 1, prop2: 2};
.
.
.
return
{
getMyObject: function(){
return anObject;
}
}
My question is why when I do something like this:
var m = new manager();
var o = m.getMyObject();
o = {a: 10, b: 20};
The actual value in 'o' which is a reference type is not changed when I do this:
var o2 = m.getMyObject();
what is actually set in the variable 'o'?
and how can I make it persist in the above
|
|
|
|

|
Your code currently does the following:
- Store the object returned from
getMyObject in a variable called o ;
- Store a new object in the
o variable;
- Store the object returned from
getMyObject in a variable called o2 ;
It should be fairly obvious why o and o2 do not point to the same object - step 2 makes o point to a new, different object.
If you want your changes to the object to be persisted for future calls of the getMyObject function, you need to change the properties of the object returned from that function:
var m = new manager();
var o = m.getMyObject();
o.prop1 = 10;
o.prop2 = 20;
var o2 = m.getMyObject();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|