Ke-mo Sah-bee
Ke-mo Sah-bee

Reputation: 1

Can't change text in button

Here is the code from the book "HTML 5 + JS for Dummies", looking at it for more than 2 hours and can't find a reason why it doesn't want to work. I'm in very early stage and sorry for my newb question.

<!DOCTYPE html>
<html>
<head>
<title> Outputting data to HTML </title>

<script language ="JavaScript">


{
document.getElementById("myText").
innerHTML ="Clicked!";
}
</script>
</head>



<body>
<h1> Creating HTML Element Output </h1>
<div> <p id="myText">Change Me </p> </div>

<div> 

<input id="btnClickMe"
type="button"
value = "Click me"
onlick="WriteText()"/>


</div>
</body>
</html>

Upvotes: 0

Views: 86

Answers (2)

Luke
Luke

Reputation: 1806

<!DOCTYPE html>
<html>
<head>
<title> Outputting data to HTML </title>

<script>

function WriteText()
{
document.getElementById("myText").
innerHTML ="Clicked!";
}
</script>

</head>


<body>
<h1> Creating HTML Element Output </h1>
<div> <p id="myText">Change Me </p> </div>

<div> 

<input id="btnClickMe"
type="button"
value = "Click me"
onclick="WriteText()"/>


</div>
</body>
</html>

Here's what i did/what was wrong:

  1. You have used wrong syntax for functions or you forgot to use the function keyword
  2. You misspelled onclick (onlick)
  3. I moved the script part out of the head

Upvotes: 1

Sandeep
Sandeep

Reputation: 1479

Use setAttribute method because input filed have not innerHTML property.

`<script language ="JavaScript">
function WriteText()
{
document.getElementById("myText").setAttribute("value", "Clicked!");
}
</script>
</head>

`

Upvotes: 0

Related Questions