Jump to content

Understanding passing of a variable to a function


BooKwiznak

Recommended Posts

In the function with a parameter exercise, I noticed the function had a txt variable inside it's parentheses. So the code is:

<html><head><script type="text/javascript">function myfunction(txt){alert(txt);}</script></head><body><form><input type="button" onclick="myfunction('Hello')" value="Call function"></form><p>By pressing the button above, a function will be called with "Hello" as a parameter. The function will alert the parameter.</p></body></html>

Inside the input type element, the onclick Event has myfunction('Hello'). This means the value or expression of the txt variable is written inside the onclick Event? Or is it just for this particular situation because the onclick Event is being used so an object must be clicked? Because I noticed in the Math section the variable value or expression was written inside the parentheses when it was being executed in the document.write(); Like so:

function product(a,{return a*b;}</script></head><body><script type="text/javascript">document.write(product(4,3));</script>

And one last thing. On the mentioned Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial. in the 'How To' section, is this where the HTML DOM properties such as .innerHTML and methods like getElementById come into play? Since it states that document.write() is bad practice? I apologize if this is really long and dragged out~! I just want to have a better grasp of everything.(p.s. I'm sure I didn't put that emoticon there but it shows up on the preview post~! Is is just my laptop?)

Link to comment
Share on other sites

In the function with a parameter exercise, I noticed the function had a txt variable inside it's parentheses. So the code is:...Inside the input type element, the onclick Event has myfunction('Hello'). This means the value or expression of the txt variable is written inside the onclick Event? Or is it just for this particular situation because the onclick Event is being used so an object must be clicked?
The variable txt is what is called a parameter or argument of the function. When you write myfunction('Hello') you are passing the string 'Hello' as an argument to the myfunction function, which means that txt now has a value of 'Hello'. It doesn't matter that the function is assigned to an onclick handler.It is the same situation with the math example you posted, except the product function has two arguments.
And one last thing. On the mentioned Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial. in the 'How To' section, is this where the HTML DOM properties such as .innerHTML and methods like getElementById come into play? Since it states that document.write() is bad practice?
That is exactly the way that innerHTML and getElementById come into play. You will often find yourself using getElementById to get a reference to an element and then change its value or innerHTML.
(p.s. I'm sure I didn't put that emoticon there but it shows up on the preview post~! Is is just my laptop?)
That's just the BBCode used on this forum. Any instance of b ) or B ) (without spaces) will turn into :) I'm not sure why it parsed the BBCode inside your [code ] tags though....
Link to comment
Share on other sites

The variable txt is what is called a parameter or argument of the function. When you write myfunction('Hello') you are passing the string 'Hello' as an argument to the myfunction function, which means that txt now has a value of 'Hello'. It doesn't matter that the function is assigned to an onclick handler.It is the same situation with the math example you posted, except the product function has two arguments.That is exactly the way that innerHTML and getElementById come into play. You will often find yourself using getElementById to get a reference to an element and then change its value or innerHTML.That's just the BBCode used on this forum. Any instance of b ) or B ) (without spaces) will turn into :) I'm not sure why it parsed the BBCode inside your [code ] tags though....
Thanks!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...