|
Below I have the code I am working with. The if statement in the script is not working. What I want to do is catch the value of the prompt dialog box if the user clicks the cancel buttom and exit the script if that is the case. I have tried:
if (color=="null")
if (color==="null")
if (color=='null')
if (color==='null')
if (color==null)
if (color===null)
and none of them work. I can make the script work if I do:
if(color!=null) and modify the logical processing
but I am trying to make work with the equals comparison.
If I comment out the if conditional statement, it works fine.
Any help is greatly appreciated. Thank you.
<script type="text/javascript">
<!--
var color = prompt("Enter a color","Enter red, white, or blue.")
function colorTest(){
if(color === null){
break;
}
switch(color){
case "red":
alert("You have entered the color ' red '");
break;
case "white":
alert("You have entered the color ' white '");
break;
case("blue"):
alert("You have entered the color ' blue '");
break;
default:
alert("You did not enter a valid color");
}
}
</script>
modified 10-Mar-14 19:32pm.
|
|
|
|
|
That break inside the if does nothing...only jumps out of the if and continues to the switch !
You meant return I think...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Just put a breakpoint and see what the value of color is.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
prompt returns a string var
to check an var take if(color!=null) ...
to check the value of var take if(color!='') and so on
break-statement is not a member of if-statement
|
|
|
|
|
Hello
I want to write the confirmation message
For example when i fill in a form and click on the button "Add" I want to get a message "Are you sure you want to add...." and when i click ok the items should be added in the database and when i click cancel the form should remain as it was..
Please help me..
Thank u
|
|
|
|
|
|
thank u but am not using asp..
i want a simple code in javascript..
SM
|
|
|
|
|
It's very simple to extract the below sample code from that page, anyway customize it & search Google for more
OnClientClick="return confirm('Are you certain you want to delete this product?');"
thatrajaCode converters | Education Needed
No thanks, I am all stocked up. - Luc Pattyn
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute
|
|
|
|
|
Hi,
Why dont you try JQuery confirm Plugin
Check it out !!
http://myclabs.github.io/jquery.confirm/[^]
Or
if (confirm('Are you sure you want to add a new row')) {
//alert('You have added a new Row');
return true;
} else {
//alert('Cancel is clicked');
return false;
}
|
|
|
|
|
Do reply to OP's message instead of mine.
If this is reply to me,
OP is a beginner so I have suggested him an easy one which is traditional using javascript. Many people used that script for delete operation inside GridView. Clickety[^]
|
|
|
|
|
Hi,
Sorry, I meant to reply OP's message.
|
|
|
|
|
Hi,
Why dont you try JQuery confirm Plugin
Check it out !!
http://myclabs.github.io/jquery.confirm/[^]
Or
if (confirm('Are you sure you want to add a new row')) {
//alert('You have added a new Row');
return true;
} else {
//alert('Cancel is clicked');
return false;
}
|
|
|
|
|
Hi,
I need help on consuming external WCF REST service and display the data using angular.js.
I have (url:http://localhost/service1.svc/flowers) that fetches WCF REST service which returns data in JSON format as given below:
[{"FlowerName":"Jasmine","Place":"Mysore"},{"FlowerName":"Rose","Place":"Bangalore"},{"FlowerName":"Sun flower","Place":"Belgaum"}]
I need to display the above data in a html with rows and columns respectively.
Can anyone please help me with suitable sample code?
Kind regards,
Ram
|
|
|
|
|
You can try to use a View template, it will fit your needs, I'm using one right now (doT.js).
It will take you some time to understand it, but after that, displaying your objects will be a game !
|
|
|
|
|
i know how to do it jquery
$.ajax({
type: "GET",
url: "http://localhost/service1.svc/flowers",
success:function(data){
var x=JSON.parse(data);
var builtHtml="<tr><td>FlowerName</td><td>Place</td></tr>";
for(i=0;i<x.length;i++)
{builtHtml+="<tr><td>"+x[i].FlowerName+"</td><td>"+x[i].Place+"</td></tr>"}
$("#tableID").append(builtHtml)
}})
But i never used angular.js before
modified 7-Mar-14 16:41pm.
|
|
|
|
|
I have been trying to get the 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 pasted your code as is into a file and it runs as it should. Perhaps you have a typing error in your actual code?
vbmike
|
|
|
|
|
BODY object in Internet Explorer, see
http://msdn.microsoft.com/en-us/library/ms535205%28v=vs.85%29.aspx
|
|
|
|
|
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>I like to listen to songs on the radio or a CD</td>
<td width="70">
<label></label>
<label>
<input name="bm1" type="radio" value="1" />
Yes</label>
</td>
<td width="72">
<label></label>
<label>
<input name="bm1" type="radio" value="2" />
No
</label>
</td>
</tr>
<tr><td>I like to watch music videos on TV</td><td>
<label>
<input name="bm2" type="radio" value="1" />
Yes</label>
</td>
<td>
<label>
<input name="bm2" type="radio" value="2" />
No </label>
</td>
</tr>
<tr><td>I like to go to music concerts and hear live music</td><td>
<label>
<input name="bm3" type="radio" value="1" />
Yes</label>
</td>
<td>
<label>
<input name="bm3" type="radio" value="2" />
No </label>
</td>
</tr>
<tr><td>I can easily remeber tunes, raps, or melodies</td><td>
<label>
<input name="bm4" type="radio" value="1" />
Yes</label>
</td>
<td>
<label>
<input name="bm4" type="radio" value="2" />
No </label>
</td>
</tr>
<tr><td>I take music lessons, singing lessons, or play a muscial instrument</td><td>
<label>
<input name="bm5" type="radio" value="1" />
Yes</label>
</td>
<td>
<label>
<input name="bm5" type="radio" value="2" />
No </label>
</td>
</tr>
<tr><td>I can learn new songs easily</td><td>
<label>
<input name="bm6" type="radio" value="1" />
Yes</label>
</td>
<td>
<label>
<input name="bm6" type="radio" value="2" />
No </label>
</td>
<tr><td>I like to sing</td><td>
<label>
<input name="bm7" type="radio" value="1" />
Yes</label>
</td>
<td>
<label>
<input name="bm7" type="radio" value="2" />
No </label>
</td>
</tr>
</table>
</form>
</body>
</html>
modified 7-Mar-14 5:35am.
|
|
|
|
|
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
|
|
|
|