I’m practicing in Javascript learning how to change text to italics, bold, etc.,.by calling a function. I’m just interested in someone clarifying the use of “this” keyword.
Here’s the code using “this”:
[CODE]
<html>
<head>
</head>
<body>
<script language=”javascript”>
function makeItalics(text1) {
document.write(“<i>” + text1 + “</i>”);
}
</script>
<input type=”text” size=”50″ name=”text1″ value=”hello” onFocus=”makeItalics(this.value)”>
</body>
</html>
When the function is called at the onFocus event, the word “hello” is italicized.
Then I tried the code as follows:
[CODE]
<html>
<head>
</head>
<body>
<script language=”javascript”>
function makeItalics() {
document.write(“<i>” + text1 + “</i>”);
}
</script>
<input type=”text” size=”50″ name=”text1″ value=”hello” onFocus=”makeItalics()”>
</body>
</html>
At the onFocus event, [object] is displayed in the browser window, in italics.
When the function is called, I’ve referenced the “text1” name in document.write, so why wouldn’t “hello” have appeared in italics?