JavaScript


28
Mar 08

How to perform mathematical operations in javascript?

You might come across a situation wherein you need to perform some mathematical operations on the data entered by the user. Be it addition, subtraction , multiplication or division, you can do these calculations using Javascript easily.

Since all the variables in JavaScript are declared using ‘var’ as a datatype, we need to use a function to tell Javascript to treat the contents of the variable as a number on which calculations have to be performed.

All you need to do is to use the ‘Number()’ function.

The example below will make things clear for you :

var a = “5″;

var b = “5″;

var result = a + b;

alert(“The result is “+result); // This will give you an output of 55 and not 10.

var calculatedResult = Number(a) + Number(b);

alert(“The mathematical result is “+calculatedResult); //This will give the result as 10.


28
Feb 08

Disable(readOnly) and Enable textbox using JavaScript

The following is a short example of a code that will enabledisable a textbox at the click of a button :

<html>

<script>

function enableDisable()

{

//This will get the HTML content of the current page

var form = document.testEnableDisable;

//The if condition checks for the value of the button.

if(form.enableDisableButton.value==”Disable”)
{

//If button value is ‘Disable’ then the textbox is made readOnly

form.textBox1.readOnly = true;

//The button value is changed to ‘Enable’

form.enableDisableButton.value=”Enable”

}

else

{

form.textBox1.readOnly = false;

form.enableDisableButton.value=”Disable”

}

}

</script>

<body>

<form name= “testEnableDisable”>

<input type = “text” id = “textBox1″ value=”Enabled”>

<input type=”button” id=”enableDisableButton” onClick=”enableDisable()” value=”Disable”>

</body>

</form>

</html>

Basically, its the readOnly value of the text box that needs to be changed using JavaScript.

Note: JavaScript is case sensitive and hence ‘readonly’ will not give you the right result…‘O’ should be in capital‘readOnly’ when you use it in JavaScript.

Click here to view the example


28
Feb 08

Allow Users to enter data into Message Box(Javascript Prompt)

Javascript has a function named ‘prompt’ which displays a message box along with a text box in it which allows users to input data.

Here’s how you achieve it :

function testPrompt()

{

var enteredData = prompt(“Type something in the text box below”,”");

alert(“You entered “+enteredData);

}

prompt() takes two parameters. The first parameter is used to display the message above the text box whereas the second parameter fills up the textbox with the values you specify(leave it blank if you want the text box to be empty)

To get a quick glimpse of the promt box copy paste the code below into your browser address bar and press enter :

Note: you’ll have to manually replace the double quotes(with double quotes again) after you copy paste the code due to the formatting in this page

javascript:var s = prompt(“Type something in the text box below”,”");alert(s);