简体   繁体   中英

How do i change the background text in javascript

</head>
<body>
<div>
<h1>Twhiter</h1>
<!-- Display Value from Text Input-->
<p id="demo"></p>
<!-- Get value from user -->
<input
placeholder="Write Your Tweet Here"
type="text"
id="tweetText"
value=""
name="tweet"
/>
<!-- Error Message-->
<small id="error"></small>
<!--Handle Click to call the function -->
<button onclick="myTweet()">
Twhit!
</button>
</div>
<script src="/index.js"></script>
</body>
</html>

Help me out pls i'm stuck with this code. just help me to figure the problem.... i can't really tell what is the problem. Thanks. My.js file is below

function myTweet() [
var tweetValue = document.getElementById(tweetText).Value;
var tweetCount = tweetValue.length;
var errorMsg = 
]
// myTweet();

It seems that you are using the wrong brackets for your function. Javascript functions are built this way:

function someFunction () {
    // do something
}

Your initialization of the variable errorMsg is also incomplete, you should give it a value.

Change your js file based on this, it should look like this:

    function myTweet() {
        var tweetValue = document.getElementById(tweetText).Value;
        var tweetCount = tweetValue.length;
        var errorMsg = "";
    }
// myTweet();

If you want to change the background color for the HTML above you can use

<body style="background-color:grey;">

It seems as if you are newbie to JavaScript as neither your syntax is correct nor is your approach, but still here your demanded code -

 function myTweet() { document.getElementById("tweetText").value = "error"; }
 </head> <body> <div> <h1>Twhiter</h1> <.-- Display Value from Text Input--> <p id="demo"></p> <!-- Get value from user --> <input placeholder="Write Your Tweet Here" type="text" id="tweetText" value="" name="tweet" /> <!--Handle Click to call the function --> <button onclick="myTweet()"> Twhit! </button> </div> <script src="/index.js"></script> </body> </html>

With this code you can change the value to "error" on click.

Btw, you can check this link for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM