944,088 Members | Top Members by Rank

Ad:
Oct 22nd, 2009
0

Change form input text using Javascript/Dom

Expand Post »
Hi there, I am wanting to find out how to add text to a hidden field in a HTML form. I want to add text from an array, the array holds the information for the caption placeholder for the images.

Basically i have an image viewer which lets you scroll through images and they have a text caption below them telling what the images are. When the user clicks an image this brings up the form and hides the imageViewer. I want the caption of the image clicked to automatically be entered into the hidden field of the form when a user decides to choose a particular photo, so that it sends this to the server script for processing.

At present i have the function
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function insertText()
  2. {
  3. //getElementById('camera').value=getElementById("captionPlaceHolder");
  4. document.getElementById('camera').value = document.getElementById('captionElement').value;
  5. }
i have commented some out to try various things.

My form input box has an id="camera" and i was told to do something like this. Give the hidden element an id="someName"...then in the JS you can change the value of it by the usual way...
getElementById('someName').value=??? where ??? is the name of the camera the user has

I have been messing about now for hours and just can't get it. I really hope someone can help me out. Im new to this by the way

Thanks again
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
colerainescotty is offline Offline
4 posts
since Mar 2008
Oct 22nd, 2009
0
Re: Change form input text using Javascript/Dom
Scotty,

I have to ask why you might want to send image captions back to the server?

The reasons I ask are that :
  • Captions are not necessarily unique. In a typical gallery for example, you could have a whole string of images with the caption "Sports Day".
  • The server should have the means of enquiring (typically from a database) the caption for each image. It must have done this to serve the gallery in the first place.
It would therefore be more normal to address each image by a unique identifier (UID), this being the primary key (or a derivative of it) against which the image is stored in the database. Less typically, the UID might be the directory/file path in which the image is stored on the server.

There are various ways to do this. Personally I would arrange for the click associated with each image to pass the image's UID to the handler function as an argument. eg. onclick="handleImageClick(12345)" . The function would then be of the form
javascript Syntax (Toggle Plain Text)
  1. function handleImageClick(uid){
  2. // whatever ...
  3. document.getElementById('imageID').value = uid;
  4. // whatever ...
  5. }

Airshow
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,527 posts
since Apr 2009
Oct 23rd, 2009
0
Re: Change form input text using Javascript/Dom
Thanks again,

Its part of a university assignment, its not a live website. solved!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
colerainescotty is offline Offline
4 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: onclick problem in IE, working in FF/other
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: [ask] JQuery preventDefault is not working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC