Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > changing the value of a hidden field

Reply
Thread Tools

changing the value of a hidden field

 
 
Ryan
Guest
Posts: n/a
 
      10-18-2004
I have a hidden field as such:

<INPUT TYPE=\"hidden\" name=xmlfield >

I have a button that i want to use to call a function to change the value:

<INPUT TYPE=submit VALUE="Display XML" Name="displayXML"
OnClick="ChangeFieldName('XML')">

here is the function

function ChangeFieldName (val) {
document.FieldName.xmlfield.value=val;};

nothing happens? The value is not set?


 
Reply With Quote
 
 
 
 
Dag Sunde
Guest
Posts: n/a
 
      10-18-2004
"Ryan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a hidden field as such:
>
> <INPUT TYPE=\"hidden\" name=xmlfield >
>
> I have a button that i want to use to call a function to change the value:
>
> <INPUT TYPE=submit VALUE="Display XML" Name="displayXML"
> OnClick="ChangeFieldName('XML')">
>
> here is the function
>
> function ChangeFieldName (val) {
> document.FieldName.xmlfield.value=val;};
>
> nothing happens? The value is not set?


function ChangeFieldName (val) {
if( document.getElementById )
document.getElementById('xmlfield').value = val;
}

--
Dag
5826'15.9" N 00846'45.5" E


 
Reply With Quote
 
 
 
 
Ryan
Guest
Posts: n/a
 
      10-18-2004
why do i need:

if( document.getElementById )
?
"Dag Sunde" <(E-Mail Removed)-way> wrote in message
news:clHcd.2528$(E-Mail Removed)...
> "Ryan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I have a hidden field as such:
> >
> > <INPUT TYPE=\"hidden\" name=xmlfield >
> >
> > I have a button that i want to use to call a function to change the

value:
> >
> > <INPUT TYPE=submit VALUE="Display XML" Name="displayXML"
> > OnClick="ChangeFieldName('XML')">
> >
> > here is the function
> >
> > function ChangeFieldName (val) {
> > document.FieldName.xmlfield.value=val;};
> >
> > nothing happens? The value is not set?

>
> function ChangeFieldName (val) {
> if( document.getElementById )
> document.getElementById('xmlfield').value = val;
> }
>
> --
> Dag
> 5826'15.9" N 00846'45.5" E
>
>



 
Reply With Quote
 
Bubba
Guest
Posts: n/a
 
      10-18-2004
Ryan wrote:
> I have a hidden field as such:
>
> <INPUT TYPE=\"hidden\" name=xmlfield >
>
> I have a button that i want to use to call a function to change the
> value:
>
> <INPUT TYPE=submit VALUE="Display XML" Name="displayXML"
> OnClick="ChangeFieldName('XML')">
>
> here is the function
>
> function ChangeFieldName (val) {
> document.FieldName.xmlfield.value=val;};
>
> nothing happens? The value is not set?


Use document.FORMNAME instead of document.FIELDNAME.

--
Bubba


 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-18-2004
On Mon, 18 Oct 2004 04:08:40 GMT, Dag Sunde <(E-Mail Removed)-way> wrote:

> "Ryan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...


[snip]

>> <INPUT TYPE=\"hidden\" name=xmlfield >


[snip]

> function ChangeFieldName (val) {
> if( document.getElementById )
> document.getElementById('xmlfield').value = val;
> }


That won't help. The control is *named*; it does not have an id, which
makes getElementById patently useless. Furthermore, as there is a form (a
hidden value would be pointless, otherwise), using getElementById makes
the page unnecessarily backward-incompatible.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-18-2004
On Mon, 18 Oct 2004 00:22:06 -0400, Ryan <(E-Mail Removed)>
wrote:

Please don't top-post.

> why do i need:
>
> if( document.getElementById )
> ?


The getElementById method is a relatively recent addition to the Document
Object Model (DOM). Older browsers don't support it, so a technique known
as feature detection is used to check that a user agent supports the
method before a script tries to execute it.

More information can be found in the FAQ (<URL:http://jibbering.com/faq/>
- look for 4.26) and it's notes
(<URL:http://www.jibbering.com/faq/faq_notes/faq_notes.html>). You'll also
find information on how to reference form controls.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Ryan Gaffuri
Guest
Posts: n/a
 
      10-18-2004
"Bubba" <(E-Mail Removed)> wrote in message news:<PsLcd.149$(E-Mail Removed)-ops.be>...
> Ryan wrote:
> > I have a hidden field as such:
> >
> > <INPUT TYPE=\"hidden\" name=xmlfield >
> >
> > I have a button that i want to use to call a function to change the
> > value:
> >
> > <INPUT TYPE=submit VALUE="Display XML" Name="displayXML"
> > OnClick="ChangeFieldName('XML')">
> >
> > here is the function
> >
> > function ChangeFieldName (val) {
> > document.FieldName.xmlfield.value=val;};
> >
> > nothing happens? The value is not set?

>



> Use document.FORMNAME instead of document.FIELDNAME.



I tried the following and nothing worked.

document.formname.xmlfield.value = val
I also tried naming my form:

<FORM name=ADDRESSBOOK>

dont know if i need to name it when i do </FORM>

then did:

document.ADDRESSBOOK.xmlfield.value = val

this is what i am trying to do. I have a form. When a certain button
is pressed. I want to pass a value back to my java servlet. I thought
the best way to do it was to use a hidden field and set that with a
button. then i can retrieve it on the servlet side.
 
Reply With Quote
 
Ryan Gaffuri
Guest
Posts: n/a
 
      10-18-2004
"Michael Winter" <(E-Mail Removed)> wrote in message news:<opsf16n8uox13kvk@atlantis>...
> On Mon, 18 Oct 2004 04:08:40 GMT, Dag Sunde <(E-Mail Removed)-way> wrote:
>
> > "Ryan" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...

>
> [snip]
>
> >> <INPUT TYPE=\"hidden\" name=xmlfield >

>
> [snip]
>
> > function ChangeFieldName (val) {
> > if( document.getElementById )
> > document.getElementById('xmlfield').value = val;
> > }

>
> That won't help. The control is *named*; it does not have an id, which
> makes getElementById patently useless. Furthermore, as there is a form (a
> hidden value would be pointless, otherwise), using getElementById makes
> the page unnecessarily backward-incompatible.
>
> Mike


i also tried the following which i got out of the gibbering faq.

document.forms["myform"].elements["xmlfield"].value=val
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-18-2004
On 18 Oct 2004 10:21:20 -0700, Ryan Gaffuri <(E-Mail Removed)> wrote:

[snip]

> I tried the following and nothing worked.
>
> document.formname.xmlfield.value = val
> I also tried naming my form:
>
> <FORM name=ADDRESSBOOK>
>
> dont know if i need to name it when i do </FORM>


No. Attributes only appear in the starting tag of an element.

Perhaps you should try validating your page
(<URL:http://validator.w3.org/>). Being unsure of something so basic
suggests you should revisit some HTML basics.

> then did:
>
> document.ADDRESSBOOK.xmlfield.value = val


Without actually seeing your page, it's diffcult to say what's wrong.
Nothing this simple should be so hard to solve. If you still need help in
this thread, please post your mark-up and the relevant script code.
Preferably this should be in the form of a simplified page, posted
somewhere on the Web. If that isn't possible, the same should be copied
and pasted in a post.

> this is what i am trying to do. I have a form. When a certain button is
> pressed. I want to pass a value back to my java servlet. I thought the
> best way to do it was to use a hidden field and set that with a button.
> then i can retrieve it on the servlet side.


From that description, and what you've posted, there's no need for
scripting at all.

Submit buttons, just like any other control, will submit a name/value pair
if the browser considers it a "successful control". There are two ways to
implement this: with a BUTTON element, or an INPUT element with type
"submit". The former will allow cleaner server-side processing, but it
won't work at all with NN4. The latter will work with all browsers, but it
will require changes to your server-side code.

<button type="submit" name="xmlfield"
value="XML">Display XML</button>

or

<input type="submit" name="xmlfield" value="Display XML">

You'll immediately notice that the two values to be submitted are
different. The problem with the INPUT element is that it's value is also
the text displayed.

Whichever of the two you'll choose, either will be a far better solution
than making your page reliant on script support.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
Populate Hidden field on post back and retrieve value from Hidden Field Rick ASP .Net 3 04-13-2010 05:38 PM
Select box value changes value of hidden field dvwool@ix.netcom.com Javascript 6 04-27-2006 04:38 AM
Pass hidden form field value to another form field to insert in db GavMc ASP General 4 09-22-2005 06:33 PM
writing from hidden field to text field Roy Adams Javascript 1 12-20-2004 03:47 AM



Advertisments