Question

Could you please help me to write a Javascript code that will do the following. Please...

Could you please help me to write a Javascript code that will do the following. Please keep it as simple as possible as I am new to coding in Javascript.

create 2 buttons. button1 and button2. If  button1 is pushed then "button1 is pressed" is displayed. If button2  is pressed, then "button2 is pressed" is displayed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

AS IT IS A FUNDAMENTAL IDEAS ON JAVASCRIPT, HERE THE JAVA SCRIPT PROGRAM IS DONE WITH TWO WAYS.

1. IN FIRST WAY MESSAGES WILL BE PRINTED ON HTML PAGE AFTER CLICKING ON THE BUTTONS.

2. IN SECOND WAY MESSAGES WILL BE PRINTED THROUGH "MESSAGE BOX" AFTER CLICKING ON THE BUTTONS.

THESE TWO PROCESSES WILL HELP TO GET COMPLETE IDEA ABOUT MESSAGE PRINT THROUGH BUTTON IN JAVASCRIPT AT FUNDAMENTAL LEVEL.

PROCESS 1: (PRINT MESSAGES ON HTML PAGE )

/* Javascript program */

<html>
<body>

<h1>BUTTON CLICK MESSAGE</h1>
<!-- button1 is created with click_bt1() function -->
<button onclick="click_bt1()">Button1</button>
<!-- button2 is created with click_bt2() function -->
<button onclick="click_bt2()">Button2</button>

<!-- p tag is used to print the messages of corresponding button click function -->
<p id="bt"></p>

<!-- Javascript code within script tag -->
<!-- here click_bt1()prints message for button1 -->
<!-- here click_bt2()prints message for button2 -->
<script>
function click_bt1() {
document.getElementById("bt").innerHTML = "button1 is pressed";
}
function click_bt2() {
document.getElementById("bt").innerHTML = "button2 is pressed";
}
</script>
<!-- end the Javascript code -->

</body>
</html>

SCREEN SHOT

OUTPUT

PROCESS 2: ( PRINT MESSAGE THROUGH MESSAGE BOX )

/* Javascript program */

<html>
<body>

<h1>BUTTON CLICK MESSAGE</h1>
<!-- button1 is created with click_bt1() function -->
<button onclick="click_bt1()">Button1</button>
<!-- button2 is created with click_bt2() function -->
<button onclick="click_bt2()">Button2</button>

<!-- p tag is uesd to print the messages of corresponding button click function -->
<p id="bt"></p>

<!-- java script code within script tag -->
<!-- here click_bt1()prints message through message box for button1 -->
<!-- here click_bt2()prints message through message boxfor button2 -->
<script>
function click_bt1() {
// print message through message box //
alert("button1 is pressed");
}
function click_bt2() {
// print message through message box //
alert("button2 is pressed");
}
</script>
<!-- end the java script code -->

</body>
</html>

SCREEN SHOT

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Could you please help me to write a Javascript code that will do the following. Please...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could you please help me write a Javascript code that will show a smiley face picture...

    Could you please help me write a Javascript code that will show a smiley face picture when I click the button. The picture should not display until the button is pressed. Here is my attempt at the code. It does not work. Please keep the code as simple as you can as I am new to coding. Please also use the <div> tag if possible, onclick and use the getElementById. Thank you <html> <body> <img id="exampleImage" src="smileyFace.png" /> <button onclick="pushButton()">Try...

  • This question relates to JavaScript. Could you please show me how to get my code below...

    This question relates to JavaScript. Could you please show me how to get my code below working. Please keep it simple. The code has a green square image.  When it is clicked the function generates a random number from 1-3. If the number generated is '1' then the green square is replaced by a picture of a tree. Please keep it as simple as possible because I am new to coding. Thanks. <html> <head>              <title></title>        <script>...

  • This question relates to Javascript. Could you please show me why my code is not working?...

    This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head>               <title></title>        <script>...

  • This question relates to Javascript. Could you please show me why my code is not working?...

    This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head>               <title></title>        <script>...

  • Please do this task using JavaScript (write code in JavaScript). It is preferable to do this...

    Please do this task using JavaScript (write code in JavaScript). It is preferable to do this task in the environment “Google Docs”. Create a table where students’ grades are stored. Let us suppose that students take 6 exams. Students’ grades range from 0 to 10. Develop commands that let us: 1. Colorize in green the names of those students whose grades are higher than 5. 2. Colorize in red the names of those students whose grades are lower than 5....

  • could you please show me what I am doing wrong in the Javascript code below. <html>...

    could you please show me what I am doing wrong in the Javascript code below. <html>   <head> <meta charset="UTF-8"> <title></title> <style> #mytext { color: white; text-align: center; } </style> <script> var words = "The cat sat on the mat"; document.writeln("id='mytext"+ words '); </script>   </head>   <body>   </body> </html>

  • In this Javascript code, why are 2 for loops needed? could you please explain this to...

    In this Javascript code, why are 2 for loops needed? could you please explain this to me. This is the outcome * ** *** **** ***** ****** ******* ******** ********* the code is <html> <head>               <title></title>        <script>                  showStars(10);               function showStars(rows){            for(var i = 0; i < rows; i++){                var pattern = "";                for(var j...

  • Please help me with the following requirements for JavaScript. Write a function to take a temperature value in Celsius a...

    Please help me with the following requirements for JavaScript. Write a function to take a temperature value in Celsius as an argument and return the equivalent temperature in Fahrenheit, basing it on the code from Hour 2. //Original Code <!DOCTYPE html> <html> <head>     <title>Fahrenheit From Celsius</title> </head> <body>     <script>         var cTemp = 100;  // temperature in Celsius         var hTemp = ((cTemp * 9) /5 ) + 32;         document.write("Temperature in Celsius: " + cTemp + " degrees<br/>");         document.write("Temperature in Fahrenheit: "...

  • Hello Sir/Madam, could you please help me for the code to write UNO CARD GAME using...

    Hello Sir/Madam, could you please help me for the code to write UNO CARD GAME using following elements using JAVA Language: 1. Use of at least three Abstract Data Types 2. Use of inheritance and polymorphism 3. Use of recursion 4. Connection and use of a simple database 5. At least one sorting algorithm 6. At least one search algorithm 7. Use of a graphical user interface. Thank you in advance!

  • Please help me with this code for javascript. <!DOCTYPE html> <html> <head> <title>Strings and Arrays</title> <script>...

    Please help me with this code for javascript. <!DOCTYPE html> <html> <head> <title>Strings and Arrays</title> <script> function wrangleArray() { var string1 = prompt("Enter: This class is going to fast");//Must be entered by the user var string1 = "";//Is this right? I want that string to change later by a series of prompt questions document.getElementById("div1").innerHTML = "<p>" + sentence + "</p>"; var words = sentence.split(" ");//I need to convert my string to an Array is this the way to do it?...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT