Inputting a value for use in a function

  • Thread starter Thread starter Trevor L.
  • Start date Start date
T

Trevor L.

I posted this under another thread but I think it was lost as the answers
related to an earlier query

I have this code
<input type="button" value="1" onClick="chgImg('1')">
<input type="button" value="2" onClick="chgImg('2')">
<input type="button" value="3" onClick="chgImg('3')">
<input type="button" value="4" onClick="chgImg('4')">

There are about 60 such buttons (although I do generate then via JS so I
don't have to type the code for each one)

I would prefer to have a text box
Enter Number
|-------------|
|-------------|
Then the number in the box is placed into the function chgImg

Here is some code modified from some on w3schools

<form action="xxx.html" method="get">
Enter Number:
<input type="text" name="number" value=" ">
<br>
<input type="submit" value="Submit">
</form>

Apparently this calls xxx.html with parameter "number" set to what was typed
in, whereas I want to call a function

I tried altering the form tag to
<form action="chgImg(number)">

But it tried to open a file chgImg(number).

Can I do what I want or do I have to set up a file which then calls chgImg?
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
You could do as you tried you would then use script to read the number
value when the page loads.
It would however be simpler to just call the chgimg() function when the
page is submitted using an onclick event in the code for the submit
button <input type="submit" onclick="imgchg(); return:'false'">.
Set the forms action to the current page.
Then change the chgimg() function so it looks for a parameter in the
forms select box.

Regards Jens Peter Karlsen. Microsoft MVP - Frontpage.
 
Re: Inputting a value for use in a functionJens,
Thank you.

I have done just what you suggested (after much experimentation and other advice from this NG).
Well I almost did the same, I didn't change the function, just the parameter used
<form>
...
<input id="no" name="no" type="text" value="" size="1">
<script type="text/javascript">document.forms[0].no.focus()</script>
<input type="button" value="Go"
onclick="chgImg(this.form['no'].value)">
...
</form>

I used type="button" not type ="submit" but it works. Using submit puts the text "Submit Query" in the button, which (IMHO) is not quite as useful

--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
You could do as you tried you would then use script to read the number value when the page loads.
It would however be simpler to just call the chgimg() function when the page is submitted using an onclick event in the code for the submit button <input type="submit" onclick="imgchg(); return:'false'">.

Set the forms action to the current page.
Then change the chgimg() function so it looks for a parameter in the forms select box.

Regards Jens Peter Karlsen. Microsoft MVP - Frontpage.
-----Original Message-----
From: Trevor L. [mailto:[email protected]]
Posted At: 2. april 2005 04:24
Posted To: microsoft.public.frontpage.client
Conversation: Inputting a value for use in a function
Subject: Inputting a value for use in a function


I posted this under another thread but I think it was lost as
the answers related to an earlier query

I have this code
<input type="button" value="1" onClick="chgImg('1')"> <input
type="button" value="2" onClick="chgImg('2')"> <input
type="button" value="3" onClick="chgImg('3')"> <input
type="button" value="4" onClick="chgImg('4')">

There are about 60 such buttons (although I do generate then
via JS so I don't have to type the code for each one)

I would prefer to have a text box
Enter Number
|-------------|
|-------------|
Then the number in the box is placed into the function chgImg

Here is some code modified from some on w3schools

<form action="xxx.html" method="get">
Enter Number:
<input type="text" name="number" value=" "> <br> <input
type="submit" value="Submit"> </form>

Apparently this calls xxx.html with parameter "number" set to
what was typed in, whereas I want to call a function

I tried altering the form tag to
<form action="chgImg(number)">

But it tried to open a file chgImg(number).

Can I do what I want or do I have to set up a file which then
calls chgImg?
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
FYI
The submit button does not just put the submit text on a button (and you can change the text on the button to anything)
It defines the button w/ a specific default form action
- send the form to the form action
(similarly for the reset button)
Any other button has no action (unless added by a script) and does not submit the form to the form defined action
See
http://www.eskimo.com/~bloo/indexdot/html/tagpages/i/inputsubmit.htm

IMHO
Your script is a kludge (probably you should spend some time learning about form fields)
Instead of a 1 px input field you should be using a hidden form field (then you don't need the script tag)
See http://www.eskimo.com/~bloo/indexdot/html/tagpages/i/inputhidden.htm

You apparently have a script named chgImg(parameter) in your page (Head Section)
Then in your form just use

<input id="no" name="no" type="hidden" value="" >
<input type="submit" value="Go" onclick="chgImg(this.form.no.value)">

Using a submit both runs the onclick script and sends the form to your Form action=
--




Re: Inputting a value for use in a functionJens,
Thank you.

I have done just what you suggested (after much experimentation and other advice from this NG).
Well I almost did the same, I didn't change the function, just the parameter used
<form>
...
<input id="no" name="no" type="text" value="" size="1">
<script type="text/javascript">document.forms[0].no.focus()</script>
<input type="button" value="Go"
onclick="chgImg(this.form['no'].value)">
...
</form>

I used type="button" not type ="submit" but it works. Using submit puts the text "Submit Query" in the button, which (IMHO) is not
quite as useful

--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
You could do as you tried you would then use script to read the number value when the page loads.
It would however be simpler to just call the chgimg() function when the page is submitted using an onclick event in the code for
the submit button <input type="submit" onclick="imgchg(); return:'false'">.

Set the forms action to the current page.
Then change the chgimg() function so it looks for a parameter in the forms select box.

Regards Jens Peter Karlsen. Microsoft MVP - Frontpage.
-----Original Message-----
From: Trevor L. [mailto:[email protected]]
Posted At: 2. april 2005 04:24
Posted To: microsoft.public.frontpage.client
Conversation: Inputting a value for use in a function
Subject: Inputting a value for use in a function


I posted this under another thread but I think it was lost as
the answers related to an earlier query

I have this code
<input type="button" value="1" onClick="chgImg('1')"> <input
type="button" value="2" onClick="chgImg('2')"> <input
type="button" value="3" onClick="chgImg('3')"> <input
type="button" value="4" onClick="chgImg('4')">

There are about 60 such buttons (although I do generate then
via JS so I don't have to type the code for each one)

I would prefer to have a text box
Enter Number
|-------------|
|-------------|
Then the number in the box is placed into the function chgImg

Here is some code modified from some on w3schools

<form action="xxx.html" method="get">
Enter Number:
<input type="text" name="number" value=" "> <br> <input
type="submit" value="Submit"> </form>

Apparently this calls xxx.html with parameter "number" set to
what was typed in, whereas I want to call a function

I tried altering the form tag to
<form action="chgImg(number)">

But it tried to open a file chgImg(number).

Can I do what I want or do I have to set up a file which then
calls chgImg?
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
Thank you,Stefan

You've been a great help.

My script doubtless is a kludge. So far, I have learnt very little about
forms. I have just picked up bits that work without actually learning
anything.

Now is clearly the time to do so (to learn, that is). I will do some
reading.

Yes, I have a function named chgImg(). I used size ="1" because the text
field was too large - I only need 2 digits, but it still doesn't get quite
as small as I want.

I tried your suggestion with type="hidden" but there was nowhere to input my
value, so I have used this
<head>
<script .....>
function getfocus(){document.forms[0].no.focus()}
....
<body onload="getfocus();">
...
<input id="no" name="no" type="text" value="" size="1">
<input type="submit" value="Go"
onclick="chgImg(this.form['no'].value );getfocus()">

I will keep plodding on
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
FYI
The submit button does not just put the submit text on a button (and
you can change the text on the button to anything)
It defines the button w/ a specific default form action
- send the form to the form action
(similarly for the reset button)
Any other button has no action (unless added by a script) and does
not submit the form to the form defined action
See
http://www.eskimo.com/~bloo/indexdot/html/tagpages/i/inputsubmit.htm

IMHO
Your script is a kludge (probably you should spend some time learning
about form fields)
Instead of a 1 px input field you should be using a hidden form field
(then you don't need the script tag)
See
http://www.eskimo.com/~bloo/indexdot/html/tagpages/i/inputhidden.htm

You apparently have a script named chgImg(parameter) in your page
(Head Section)
Then in your form just use

<input id="no" name="no" type="hidden" value="" >
<input type="submit" value="Go" onclick="chgImg(this.form.no.value)">

Using a submit both runs the onclick script and sends the form to
your Form action= --




Re: Inputting a value for use in a functionJens,
Thank you.

I have done just what you suggested (after much experimentation and
other advice from this NG).
Well I almost did the same, I didn't change the function, just the
parameter used <form>
..
<input id="no" name="no" type="text" value="" size="1">
<script type="text/javascript">document.forms[0].no.focus()</script>
<input type="button" value="Go"
onclick="chgImg(this.form['no'].value)">
..
</form>

I used type="button" not type ="submit" but it works. Using submit
puts the text "Submit Query" in the button, which (IMHO) is not quite
as useful


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top