homepage Welcome to WebmasterWorld Guest from 54.158.163.106
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript in hidden form field help
epsd

10+ Year Member



 
Msg#: 3467459 posted 9:48 pm on Oct 2, 2007 (gmt 0)

I've got some JS that gets the page title like so:
document.write (document.title)

works fine.

I've got a form with a hidden field like so:
<input type="hidden" name="linktitle" value="HELP"></form>

How can I get my JS to execute in the value part (where it says HELP)? I've tried numerous methods and none have worked. Any ideas? Thanks!

 

Arno_Adams

10+ Year Member



 
Msg#: 3467459 posted 6:39 am on Oct 3, 2007 (gmt 0)

Hi,

you can't execute the value of the hidden form field, but you can reach it via:

[window.]document.formName.elementName.value
[window.]document.forms[i].elements[i].value
[window.]document.getElementById("elementID").value

HTH, AA

epsd

10+ Year Member



 
Msg#: 3467459 posted 11:16 am on Oct 3, 2007 (gmt 0)

Thanks for your help. Could you clarify a bit for me on how to implement that with form?

epsd

10+ Year Member



 
Msg#: 3467459 posted 12:24 pm on Oct 3, 2007 (gmt 0)

Okay, I see now what you were saying. So I tried this

document.formtwo.linktitle.value = 'document.title';

formtwo is the name of the form, linktitle is the name of the hidden field. However, the returned value is then document.title and not the actual title... I tried it without quotes and got nothing. I also tried:

document.write(document.formtwo.linktitle.value+document.title);

an it returned nothing. What am I missing?

Trace

5+ Year Member



 
Msg#: 3467459 posted 1:13 pm on Oct 3, 2007 (gmt 0)

I'm trying my best to understand what it is you actually want to accomplish but I just don't get it. Something like this? Why would you want to do that?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> This is my title </title>
</head>
<body>

<form name="myForm">
<input type="hidden" name="linktitle" value="HELP" />
</form>

<script>
document.myForm.linktitle.value = document.title;
alert(document.myForm.linktitle.value);
</script>

</body>
</html>


epsd

10+ Year Member



 
Msg#: 3467459 posted 1:24 pm on Oct 3, 2007 (gmt 0)

I need to get the html page title into the value of a hidden form field which I then get via a POST when the form is submitted. Seems simple but I just can't get it to work.

Trace

5+ Year Member



 
Msg#: 3467459 posted 2:21 pm on Oct 3, 2007 (gmt 0)

Did you try my example? It places the title in the hidden field.

epsd

10+ Year Member



 
Msg#: 3467459 posted 2:32 pm on Oct 3, 2007 (gmt 0)

Yes, I tried it. I get a return of UNDEFINED.

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3467459 posted 4:20 pm on Oct 3, 2007 (gmt 0)

1. If this is critical, then you should not be relying on JavaScript to populate your hidden field. If the user has JavaScript disabled, you won't get a value.

2. If you're still going to use JavaScript, you need to wait for the window to finish loading, as the document.title might not be available until the DOM is ready.

Try this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>This is my title</title>
</head>
<body>
<form name="myForm">
<div>
<input type="hidden" id="linktitle" name="linktitle" value="" />
</div>
</form>
<script type="text/javascript">
window.onload = function() {
document.getElementById('linktitle').value = document.title;
};
</script>
</body>
</html>

epsd

10+ Year Member



 
Msg#: 3467459 posted 4:56 pm on Oct 3, 2007 (gmt 0)

The window has loaded. Then the user presses the form button. The above didn't work...

EDIT- after some playing around with it (where to place it) it is working now. THANKS!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved