Forum Moderators: open
function myfunction(txt)
{
alert(txt);
}
Why can't it display the alert without the "txt" argument?
Am I overlooking something here?
Also, it seems like you can achieve the same result more efficiently with this method:
[w3schools.com...]
So please, enlighten me on what role the argument plays and how it's useful.
Thanks!
function myfunction(txt)
{
alert("You clicked " + txt);
}
<input type="button"
onclick="myfunction(this.value)"
value="Save">
<input type="button"
onclick="myfunction(this.value)"
value="Cancel">
If you had simply hard coded a value to be alerted, then your function is not really re-usable.
I suppose what confuses me the most is that "txt" never shows up anywhere. I would understand if the button displayed "txt" when you click it, but it does not. It displays "Hello".
Unless you are saying that all messages like "Hello" are sub-sets of "txt". It just seems to me like I should be able to delete "txt" altogether and still have it work - but that is not the case.
If you are not passing any information to the function, then it's not required. For example:
function myfunction()
{
alert("You called myfunction");
}
For example, maybe I want to create a function that takes a phone number as the parameter. I might do something like this:
function validatePhoneNum(phoneNumber) {
}
Within my function, I can now reference the value passed using that phoneNumber variable. Maybe I also want to pass in boolean value to indicate whether or not I allow string data in my phoneNumber:
function validatePhoneNum(phoneNumber, allowStrings) {
}
So when I call this function like this:
validatePhoneNum("000-0000", false);
I'm passing the value "000-0000" as the phoneNumber variable, and false as the allowString variable. I might call that function again with different values:
validatePhoneNum("1111", true);
Make sense?
<script type="text/javascript">
function myfunction(pipeline1,pipeline2,pipeline3) {
var p2_x_p3 = pipeline2*pipeline3;
alert(pipeline1 + p2_x_p3);
}myfunction("The value of pipeline2 x pipeline3 is: ",21,67);
/***
Now, you don't even have to assign arguments to the function directly within it's parens (though normally you should if using arguments).
You can still access the functions arguments property (from within the function).
(A function's arguments property is a type of array, but not really like other arrays,
it doesn't have the methods other javascript arrays have, but you do access it's members by index number).
***/
function myfunction2() { //no arguments properties assigned to any names within the functions parens ()
if (arguments[0]) {
alert(arguments[0]);
}
if (arguments[1]) {
alert(arguments[1]);
}
}myfunction2("This is myfunction2's arguments[0]", "This is arguments[1]");
</script>
If you don't specify txt (or whatever variable name), then you don't have any way of accessing that information.
@LifeinAsia: your comment about information being passed to the function made me re-examine the flow of the code. Now I see that
onclick="myfunction('Hello')"
sends the value of
'Hello'
to
function myfunction(txt)
and then from there
function myfunction(txt)
passes that value on to
alert(txt);
Got it!
@Fotiman: This comment also helped to clarify:
"You need some way to reference the parameters that you pass to your functions."
@astupidname: Thanks for the encouraging words and example.