Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get onclick button's value 1

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
Hi all,

I changed a form function from selected drop down box options to buttons and now hit a wall because this script does not capture selected value anymore
Code:
Channel.selectedIndex
Even I know very little about js, I know enough the script above is capturing drop down value only. So, what can I change for the code to get the value from the buttons instead.

Thanks!
 
Hi

Like this ?
HTML:
<input type="button" value="One" onclick="fu(this)">
<input type="button" value="Two" onclick="fu(this)">
<input type="button" value="Three" onclick="fu(this)">
JavaScript:
function fu(what)
{
  alert('Button '+what.value+' pressed')
}

Feherke.
 
FYI

Code:
Channel.selectedIndex

would grab the selected index (i.e. which option was selected 0,1,2 regardless of the value).

you'll need to use this to get the actual value

Code:
Channel.selectedIndex.value

I.e.

<select>
<otion value="who">who</option>
<otion value="what" selected>what</option>
<otion value="where">where</option>
</select>

Your method would return "1" (it's zero based) whereas the actual selected value is "what"


TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Thank you Feherke,

This is a next step to the above question.
There is another Submit button that will pick up value from the drop down box that was selected earlier. Now that I changed to buttons, how can I capture that selected button value when hit Submit. Do you think cookie will be required in this case?
 
Thanks vicvirk,

The matter of fact is that the js script I'm working with does not even need to have "Channel.selectedIndex.value". All they do to capture value is "Channel.value" and it works. I wonder why?
 
channel.value will work too - it depends on how the object was obtained..



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Hi

You mean
[ol]
[li]click on an [tt]input type="button"[/tt][/li]
[li]click on an [tt]input type="submit"[/tt][/li]
[/ol]
And the submitted [tt]form[/tt] data should include the [tt]value[/tt] of the [tt]button[/tt] pressed in step 1) too ?

No need for cookie, ( although that could also be a way ) a [tt]hidden[/tt] [tt]input[/tt] is enough :
HTML:
<form action="#">
[red]<input type="hidden" name="pressed">[/red]
<input type="button" value="One" onclick="fu(this)">
<input type="button" value="Two" onclick="fu(this)">
<input type="button" value="Three" onclick="fu(this)">
[red]<input type="submit">[/red]
</form>
JavaScript:
function fu(what)
{
  [red]what.form.pressed.value[/red]=what.value
}


Feherke.
 
Hi

vicvirk said:
you'll need to use this to get the actual value
Code:
Channel.selectedIndex.value
Let us analyze your code abit :
Code:
<select [red]onchange="alert(typeof this.selectedIndex)"[/red]>
<option value="who">who</option>
<option value="what" selected>what</option>
<option value="where">where</option>
</select>
And that says [tt]number[/tt]. So [tt]selectedIndex[/tt] can not have [tt]value[/tt] property.

Feherke.
 
:) I'm lost Feherke.

Basically I understand what you're saying on the above, but how can I on the other end of the form retrieve the pressed value or, more importantly, retrieve such value from another function that activated by another button within this form?
 
Hi

cumap said:
how can I on the other end of the form retrieve the pressed value
[ol]
[li]load the document containing the [tt]form[/tt] I posted previously in the browser[/li]
[li]press [tt]button[/tt] "Two"[/li]
[li]press [tt]submit[/tt][/li]
[li]in PHP running [tt]print_r[/tt]($_GET); you will get this :
Code:
Array
(
    [pressed] => Two
)
[/li]
[/ol]
So this code :
Code:
echo "Button $_GET[pressed] pressed";
will output "Button Two pressed".

If I understand "the other end of the form" correctly...

Feherke.
 
Thank you Feherke. you've been a very big help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top