views:

37

answers:

4

I know that this is an embarassingly easy question, but I can't figure out the problem, and that's why I'm asking the question, so please don't reiterate this point.

Anyway, I'm just working on something here, and when I tested my page to see how things were going, I realized that my calculate() method isn't clearing text input like I want it to.

Here is the markup and the script:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
function calculate(){
var valuea = document.form1.variablea.value;
var valueb = document.form1.variableb.value;
var valuec = document.form1.variablec.value;
document.form1.variablea.value = "";
document.form1.variableb.value = "";
document.form1.variablec.value = "";
}
</script>
</head>

<body>
<form name="form1">
    a:<input name="variablea" value="" type="text">
    <br/>
    b:<input name="variableb" value="" type="text">
    <br/>
    c:<input name="variablec" value="" type="text">
    <br/>
    <input name="calculate" value="Calculate!" type="button" onClick="calculate()">
</form>
</body>
</html>

Please tell me if you see anything.

+1  A: 

Not very sure, but if you don't want to move to jQuery here's what you could try:

function calculate() {
    var inputa = document.getElementById('inputa');
    inputa.value = '';
}

Just test this, having an id "inputa" on one of the input boxes. I only know how to get elements by id, name or tag in raw Js. Of course, you could then extend your code to what you want using one of these methods to get your form elements.

Claudiu
Thanks for the `getElementById`, I forgot about the simplest of things, a hell of a lot easier than using the other method.
BOSS
no worries :) glad you sorted it out
Claudiu
+2  A: 

You might want to try using another name. I tried to call the "calculate" function but it keeps on giving me an error saying "calculate" is not a function. But when I call the function "calculateQuad" and change the onClick event to call "calculateQuad" it works.

girdus
Hey thanks! Good idea, perhaps `calculate` is a keyword, reserved name or whatever terminology JS uses. Thanks for that! :) Edit: Disregard this, because Christian's answer shows the real reason.
BOSS
+1  A: 

Inside the onclick method is there a reference to the item you clicked. It is named the same as the name you put on the item, "calculate". This results in that "calculate" does not refer to the function, but the input tag.

To resolve this by either typing

onclick = "window.calculate()"

or rename the name of either the input-tag or the function.

Christian
@Christian any docs/specs explaning the issue ?
Rajat
OH! I got you, makes perfect sense now, disregard my comment on my selected answer.
BOSS
@BOSS Please check the right answer for everyone.
Rajat
@Rajat Unfortunately not, just what I found out after some testing. Have I learned something new today too.I'll be back with it if I can find it.
Christian
A: 

change the name of the input button to something else:

<input name="calcul" value="Calculate!" type="button" onClick="calculate()">

and it works. Since the calculate function is residing directly under the global object, I have a weird feeling your name attribute is somehow overwriting it.

Just throwing this out there. I will take a deeper look at why this is happening though.

Rajat