Jump to content

[SOLVED] Forms, changing the text in an input box


Nuggit

Recommended Posts

I feel pretty dumb because it seems simple enough.

 

What I'm trying to do:

You pick a filename from the list, and javascript detects it and makes what you picked appear in the input box right beside it.

 

This is the form, pretty much (after the PHP code makes it):

<form action="" method="get">

<select name="fl">
<option value = "index" onClick="pickedFile(index)" selected>index</option>
<option value = "general" onClick="pickedFile(general)">general</option>
<option value = "test" onClick="pickedFile(test)">test</option>
</select>

<input type="text" name="f" id="fileinput" value="" />

<input type="submit" value="Open" /></form>

 

And the function pickedFile():

function pickedFile(pickd)
{
document.getElementById('fileinput').value = pickd;
}

 

I tried learning Javascript once, but for some reason I just don't click with that whole language. Any help is appreciated!

Link to comment
Share on other sites

Unless you pick an image, and set the src, this won't work.

Nah, that has nothing to do with what I'm doing. If you're curious, this is part of a notepad-like thing on my site, and I'm trying to make it as quick as possible to open files. Before I just had it so that you typed into the [input] box the .txt file you wanted to open, but now I want to have it so that you can choose from a drop-down list. I made the drop-down list and I've figured out all the PHP that goes with it to keep it up to date.

 

My question is simply: How do you make it so that whatever you pick from the drop-down list gets mirrored into the input box?

So like, the drop down list has options "index", "general", and "test" in it. If you choose "general", then the word "general" suddenly appears in the input box.

 

Sorry if I'm unclear... I'd post the link to the page here, but there's a security hole that I know about that I don't feel up to fixing right now. ^^;;

Link to comment
Share on other sites

options doesn't have an onclick event, only the select menu with an onchange event. This might work

 

<select name="fl" onChange="pickedFile(this.value)" >
<option value = "index" selected>index</option>
<option value = "general">general</option>
<option value = "test">test</option>
</select>

Link to comment
Share on other sites

not sure which part didn't work, I just copied this code and it worked for me,

<select name="fl" onChange="pickedFile(this.value)" >
<option value = "index" selected>index</option>
<option value = "general">general</option>
<option value = "test">test</option>
</select>
<input type="text" name="f" id="fileinput" value="" />
<script language="javascript">
function pickedFile(pickd)
{
document.getElementById('fileinput').value = pickd;
}
</script>

 

Let me know which part didn't work.

Link to comment
Share on other sites

Ah. I copy-pasted exactly that code to a new page and compared the sources, and first I corrected this error

<script type="javascript">

and the script was in the head. When I moved it right below the input like you did, it started working. (I thought javascript functions were supposed to be in the head...)

 

But yay! It works.

 

Thank you, nogray!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.