2

When I click on Button, I want the input text value change to 'hello', but it doesn't seem to work, here is my code. Pros please help I'm really noob, thanks in advance!

<script type="text/javascript">
function selectfile(){
    document.uploadform.selectfile.value='hello';
}
</script>


<body>
<form name="uploadform" id="uploadform">
    <input type="text" name="selectfile" id="selectfile" value="Hi"/>
    <input name="upload_file" type="button" onClick="selectfile()" value="Button"/>
</form>
</body>
2
  • 1
    what error are you getting? do you know about the developer console (f12 in firefox or chrome)? Commented Jul 21, 2012 at 20:49
  • I got: Uncaught TypeError: object is not a function O__O Commented Jul 21, 2012 at 21:02

2 Answers 2

2

Please see this example I put up on jsfiddle:

function selectfile() {
    var form = document.forms['uploadform'];
    form.elements["selectfile"].value = 'Hello';
}

var uploadButton = document.getElementById("upload-button");
uploadButton.addEventListener("click",selectfile);

In the handler function I first get the form by its id and then set the value of the element with name selectfile (your input element). The event listener to the button is added using addEventListener.

The problem in your code is, that you called both the function and the element selectfile. So you are getting the "selectfile is not a function" error. Rename one of them and it works.

Sign up to request clarification or add additional context in comments.

2 Comments

For me the example works. You click the button and the input element's value changes to "Hello". Are you getting any errors in the console (Firebug/Developer Tools)? Which browser do you use?
Please see my edit describing what is wrong in your code. You can see it working here. I still think however, that it's better (i.e. cleaner) to add the handler in you JavaScript code and not in your HTML.
1

That should be working. Is that the full HTML you're using? If so, you need to close your tags. It should look something like this:

<html>
<head>
    <script type="text/javascript">
    function selectfile(){
    document.uploadform.selectfile.value='hello';
    }
    </script>
</head>


<body>
    <form name="uploadform" id="uploadform">
        <input type="text" name="selectfile" id="selectfile" value="Hi"/>
        <input name="upload_file" type="button" onClick="selectfile()" value="Button"/>
    </form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.