Can I use alert box in return statement?
up vote
0
down vote
favorite
This is my code:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name<input type="text" id="myname"><br> Phone
<input type="text" id="myphone"><br> Country <input type="text" id="mycontry"><br> Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
If yes, why don't I get expected result? Can anyone please see my code and correct me?
javascript
|
show 2 more comments
up vote
0
down vote
favorite
This is my code:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name<input type="text" id="myname"><br> Phone
<input type="text" id="myphone"><br> Country <input type="text" id="mycontry"><br> Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
If yes, why don't I get expected result? Can anyone please see my code and correct me?
javascript
alert
is a browser's way to notify user of something. It does not return anything
– Rajesh
Nov 8 at 9:14
Please explain more of what you are trying to do, and perhaps more of your code and we might be able to help. As a guess, try toreturn alert(name + " , " + phone + " , " + country + " , " + mail);
, then you could runalert(myFunction());
– Tim Rooke
Nov 8 at 9:29
alert()
does not return anything. I guess you could say that it returnsundefined
– marvinIsSacul
Nov 8 at 9:35
Firstly thanks a lot.
– user10493493
Nov 9 at 5:28
Actually I have to read user registrations details and show them in alert box.
– user10493493
Nov 9 at 6:14
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is my code:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name<input type="text" id="myname"><br> Phone
<input type="text" id="myphone"><br> Country <input type="text" id="mycontry"><br> Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
If yes, why don't I get expected result? Can anyone please see my code and correct me?
javascript
This is my code:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name<input type="text" id="myname"><br> Phone
<input type="text" id="myphone"><br> Country <input type="text" id="mycontry"><br> Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
If yes, why don't I get expected result? Can anyone please see my code and correct me?
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name<input type="text" id="myname"><br> Phone
<input type="text" id="myphone"><br> Country <input type="text" id="mycontry"><br> Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name<input type="text" id="myname"><br> Phone
<input type="text" id="myphone"><br> Country <input type="text" id="mycontry"><br> Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
javascript
javascript
edited Nov 9 at 7:53
barbsan
2,1481022
2,1481022
asked Nov 8 at 9:10
user10493493
35
35
alert
is a browser's way to notify user of something. It does not return anything
– Rajesh
Nov 8 at 9:14
Please explain more of what you are trying to do, and perhaps more of your code and we might be able to help. As a guess, try toreturn alert(name + " , " + phone + " , " + country + " , " + mail);
, then you could runalert(myFunction());
– Tim Rooke
Nov 8 at 9:29
alert()
does not return anything. I guess you could say that it returnsundefined
– marvinIsSacul
Nov 8 at 9:35
Firstly thanks a lot.
– user10493493
Nov 9 at 5:28
Actually I have to read user registrations details and show them in alert box.
– user10493493
Nov 9 at 6:14
|
show 2 more comments
alert
is a browser's way to notify user of something. It does not return anything
– Rajesh
Nov 8 at 9:14
Please explain more of what you are trying to do, and perhaps more of your code and we might be able to help. As a guess, try toreturn alert(name + " , " + phone + " , " + country + " , " + mail);
, then you could runalert(myFunction());
– Tim Rooke
Nov 8 at 9:29
alert()
does not return anything. I guess you could say that it returnsundefined
– marvinIsSacul
Nov 8 at 9:35
Firstly thanks a lot.
– user10493493
Nov 9 at 5:28
Actually I have to read user registrations details and show them in alert box.
– user10493493
Nov 9 at 6:14
alert
is a browser's way to notify user of something. It does not return anything– Rajesh
Nov 8 at 9:14
alert
is a browser's way to notify user of something. It does not return anything– Rajesh
Nov 8 at 9:14
Please explain more of what you are trying to do, and perhaps more of your code and we might be able to help. As a guess, try to
return alert(name + " , " + phone + " , " + country + " , " + mail);
, then you could run alert(myFunction());
– Tim Rooke
Nov 8 at 9:29
Please explain more of what you are trying to do, and perhaps more of your code and we might be able to help. As a guess, try to
return alert(name + " , " + phone + " , " + country + " , " + mail);
, then you could run alert(myFunction());
– Tim Rooke
Nov 8 at 9:29
alert()
does not return anything. I guess you could say that it returns undefined
– marvinIsSacul
Nov 8 at 9:35
alert()
does not return anything. I guess you could say that it returns undefined
– marvinIsSacul
Nov 8 at 9:35
Firstly thanks a lot.
– user10493493
Nov 9 at 5:28
Firstly thanks a lot.
– user10493493
Nov 9 at 5:28
Actually I have to read user registrations details and show them in alert box.
– user10493493
Nov 9 at 6:14
Actually I have to read user registrations details and show them in alert box.
– user10493493
Nov 9 at 6:14
|
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Here is your working code. You were selecting the wrong id:
change <input type="text" id="mycontry" /
> to id="mycountry"
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
add a comment |
up vote
0
down vote
If you want to use, you can use return with alert()
function. But, you cannot get any field's value from alert()
. If you want to get fields value from myFunction()
. You have to create objects.
e.g.
//create constructor
function myFunction(myname, myphone, mycountry, mymail) {
this.name = myname;
this.phone = myphone;
this.country = mycountry;
this.mail = mymail;
}
//create object
var obj = new myFunction("testName", "testPhone", "testCountry", "testMail");
window.onload = alert(obj.name + " , " + obj.phone + " , " + obj.country + " , " + obj.mail);
About JavaScript - object's functions referencing its fields
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
add a comment |
up vote
0
down vote
You don't have to return anything since you're not using return value anywhere. You can just alert:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
Notice that your actual problem was with country field: (your current code throws an error sating cannot read property value of null since the selected id is wrong)
<input type="text" id="mycountry">
<!-- was missing u in here ^^ -->
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Here is your working code. You were selecting the wrong id:
change <input type="text" id="mycontry" /
> to id="mycountry"
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
add a comment |
up vote
0
down vote
accepted
Here is your working code. You were selecting the wrong id:
change <input type="text" id="mycontry" /
> to id="mycountry"
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Here is your working code. You were selecting the wrong id:
change <input type="text" id="mycontry" /
> to id="mycountry"
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
Here is your working code. You were selecting the wrong id:
change <input type="text" id="mycontry" /
> to id="mycountry"
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
//var phone = document.getElementById('myphone').value;
//var country = document.getElementById('mycountry').value;
//var mail = document.getElementById('mymail').value;
return alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name <input type="text" id="myname" ><br>
Phone <input type="text" id="myphone" ><br>
Country <input type="text" id="mycountry"><br>
Mail <input type="text" id="mymail" value=""><br>
<button onclick="myFunction()">Try it</button>
</div>
answered Nov 9 at 8:15
Nelson Owalo
1,015925
1,015925
add a comment |
add a comment |
up vote
0
down vote
If you want to use, you can use return with alert()
function. But, you cannot get any field's value from alert()
. If you want to get fields value from myFunction()
. You have to create objects.
e.g.
//create constructor
function myFunction(myname, myphone, mycountry, mymail) {
this.name = myname;
this.phone = myphone;
this.country = mycountry;
this.mail = mymail;
}
//create object
var obj = new myFunction("testName", "testPhone", "testCountry", "testMail");
window.onload = alert(obj.name + " , " + obj.phone + " , " + obj.country + " , " + obj.mail);
About JavaScript - object's functions referencing its fields
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
add a comment |
up vote
0
down vote
If you want to use, you can use return with alert()
function. But, you cannot get any field's value from alert()
. If you want to get fields value from myFunction()
. You have to create objects.
e.g.
//create constructor
function myFunction(myname, myphone, mycountry, mymail) {
this.name = myname;
this.phone = myphone;
this.country = mycountry;
this.mail = mymail;
}
//create object
var obj = new myFunction("testName", "testPhone", "testCountry", "testMail");
window.onload = alert(obj.name + " , " + obj.phone + " , " + obj.country + " , " + obj.mail);
About JavaScript - object's functions referencing its fields
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
add a comment |
up vote
0
down vote
up vote
0
down vote
If you want to use, you can use return with alert()
function. But, you cannot get any field's value from alert()
. If you want to get fields value from myFunction()
. You have to create objects.
e.g.
//create constructor
function myFunction(myname, myphone, mycountry, mymail) {
this.name = myname;
this.phone = myphone;
this.country = mycountry;
this.mail = mymail;
}
//create object
var obj = new myFunction("testName", "testPhone", "testCountry", "testMail");
window.onload = alert(obj.name + " , " + obj.phone + " , " + obj.country + " , " + obj.mail);
About JavaScript - object's functions referencing its fields
If you want to use, you can use return with alert()
function. But, you cannot get any field's value from alert()
. If you want to get fields value from myFunction()
. You have to create objects.
e.g.
//create constructor
function myFunction(myname, myphone, mycountry, mymail) {
this.name = myname;
this.phone = myphone;
this.country = mycountry;
this.mail = mymail;
}
//create object
var obj = new myFunction("testName", "testPhone", "testCountry", "testMail");
window.onload = alert(obj.name + " , " + obj.phone + " , " + obj.country + " , " + obj.mail);
About JavaScript - object's functions referencing its fields
edited Nov 8 at 10:13
answered Nov 8 at 10:02
Alice
6771820
6771820
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
add a comment |
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
It is not working. Actually I have to do this :-Read user registration details and show them in an alert box.
– user10493493
Nov 9 at 7:12
add a comment |
up vote
0
down vote
You don't have to return anything since you're not using return value anywhere. You can just alert:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
Notice that your actual problem was with country field: (your current code throws an error sating cannot read property value of null since the selected id is wrong)
<input type="text" id="mycountry">
<!-- was missing u in here ^^ -->
add a comment |
up vote
0
down vote
You don't have to return anything since you're not using return value anywhere. You can just alert:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
Notice that your actual problem was with country field: (your current code throws an error sating cannot read property value of null since the selected id is wrong)
<input type="text" id="mycountry">
<!-- was missing u in here ^^ -->
add a comment |
up vote
0
down vote
up vote
0
down vote
You don't have to return anything since you're not using return value anywhere. You can just alert:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
Notice that your actual problem was with country field: (your current code throws an error sating cannot read property value of null since the selected id is wrong)
<input type="text" id="mycountry">
<!-- was missing u in here ^^ -->
You don't have to return anything since you're not using return value anywhere. You can just alert:
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
Notice that your actual problem was with country field: (your current code throws an error sating cannot read property value of null since the selected id is wrong)
<input type="text" id="mycountry">
<!-- was missing u in here ^^ -->
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
function myFunction() {
var name = document.getElementById('myname').value;
var phone = document.getElementById('myphone').value;
var country = document.getElementById('mycountry').value;
var mail = document.getElementById('mymail').value;
alert(name + " , " + phone + " , " + country + " , " + mail);
}
<div>
Name
<input type="text" id="myname"><br>
Phone
<input type="text" id="myphone"><br>
Country
<input type="text" id="mycountry"><br>
Mail
<input type="text" id="mymail"><br>
<button onclick="myFunction()">Try it</button>
</div>
answered Nov 9 at 8:18
Bhojendra Rauniyar
50.1k1978121
50.1k1978121
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204548%2fcan-i-use-alert-box-in-return-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
alert
is a browser's way to notify user of something. It does not return anything– Rajesh
Nov 8 at 9:14
Please explain more of what you are trying to do, and perhaps more of your code and we might be able to help. As a guess, try to
return alert(name + " , " + phone + " , " + country + " , " + mail);
, then you could runalert(myFunction());
– Tim Rooke
Nov 8 at 9:29
alert()
does not return anything. I guess you could say that it returnsundefined
– marvinIsSacul
Nov 8 at 9:35
Firstly thanks a lot.
– user10493493
Nov 9 at 5:28
Actually I have to read user registrations details and show them in alert box.
– user10493493
Nov 9 at 6:14