Javascript input output

Javascript input output Methods

Javascript input output methods  get inputs from users and display the result using the browser’s window.

Javascript input output methods.

We can use different input and output methods. Let first look into some of the ways we can display outputs to users using Javascript.

1.Javascript output methods

Below is a list of methods  Javascript uses to display results.

  • document.write().
  • window.alert().
  •  console.log().
  •  innerHTML

1.Using document.writer() method

document.writer() method mostly perform testing, because it will delete all HTML content when HTML document loads.Run below example codes.

Output

Javascript input output Methods -document.writer()

Using the document.write() method after the HTML document has loaded deletes all HTML.Run below example code.

Output.

 

using javascript document.write()
How to display using javascript document.write()

Result after clicking the button.

2.Using window.alert() method

It’s pretty easy to use javascript alert box to display data.

3. Using console.log() method

console.log  performs debugging tasks by displaying data. To open the debugging console, hit F12 key then click console. Run below example code.

4.Using innerHTML method

Javascript uses document.getElementById(elementId).innerHTML  method to have access to HTML elements.We change the content display by the element on runtime or when a user performs a certain action, For Example.

Changing element content on run-time

Changing element content if a user performs an action

2.Javascript input methods

Below are some of the ways we can get user inputs using Javascript.

  • prompt () method.
  • confirm () method.
  • Javascript HTML input method.

1.Using prompt () method

  • prompt() method displays a dialog box asking a user to enter input.The prompt method has two options, Ok and cancel.
  • If the user clicks OK, the value entered is returned and if cancel is clicked, the method returns ‘null‘.
  • Syntax
text is the value that is displayed in the dialog box and defaultText is the initial text that appears before the user enters the input.
  • Example

2.Using confirm () method

confirm () method is used when you want a user to accept something before proceeding.A dialog box appears with Ok and cancel button. If a user clicks ok, the method returns true and cancel, false.

Syntax

Example

3.Using Javascript HTML input method.

We can use javascript document.getElementByID().value method to get user inputs entered in a HTML input textbox .Read more about Input text value

Example

More Javascript tutorials

JavaScript tutorial -Basic Guide for Beginners