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!

submit innerHTML of <div> with form? 2

Status
Not open for further replies.

tas2826

Programmer
Jul 6, 2005
26
US
Can the innerHTML of a div be submitted with a form on a page?

So for example, I have:

<div id=mydiv>SOME TEXT</div>

So, mydiv.innerHTML = SOME TEXT. Let's say mydiv is an element inside of the <form> tags of the page. When the page is submitted, how do I get the value SOME TEXT to go with it? Basically, can it perform as though it were a <textarea> and be submitted with the page?

Thanks in advance,

Troy
 
No it cannot as it is not a INPUT field.

However, you can use some JavaScript and a hidden field to accomplish this. On the form use the onsubmit function to call a JavaScript method that will take the text of the <div> and place it into the hidden field... then submit the form.
 
You could ise Javascript to do this. You'd have to trap the onSubmit event, get the value of the div

Code:
var output = document.getElementById('mydiv').innerHTML

add it to the querystring/postbody and submit via javascript. One way to add it would be to create a hidden field on the fly with JS, then submit with form.submit();

Scott Prelewicz
Web Developer
COMAND Solutions
 
Yes, that is another alternative. All depends on what approach you want to do. I personally would rather grab the value and place it into a hidden field and submit it. This will just send the value through the querystring/postbody without having to try and manipulate it.

for instance:
Code:
<form ... onsubmit="return addHidden()"... >

    <input type="hidden" id="output" />

</form>
<script>
function addHidden() {
    var divValue = document.getElementById("divId").value;
    document.getElementById("output").value = divValue;
    return true;
}
</script>
 
Hey Mikey,

We're generally on the same page. I didnt even see your reply before I made mine.

I personally dont like having to manipulate the DOM either. It would be the OP preference, but now he should have enough info to go forward.

Scott Prelewicz
Web Developer
COMAND Solutions
 
I'm sure that's all well and good, but what possible use can there be to submitting static text?

On the other hand if the text is dynamically created why use JS, when you can just as easily populate a hidden field at the time you generate the div, or even better just recreate it when you process the form.

I just don't see any useful implementation of submitting text and involving JS which is potentially subject to fail when you have previously generated it in some fashion, and that should be reproducible at the moment you process the form.

Or already be in a form field by the time the form is being filled in.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Those are all good points. I assumed he had good reason to do what he wants, and now he knows to do what he wants.

But after reading your post he'll maybe find a better way to do it.

Scott Prelewicz
Web Developer
COMAND Solutions
 
I'm sure that's all well and good, but what possible use can there be to submitting static text?

Thought I submitted this before, but did not post for some reason. Anyway, I do have a reason for needing to send the text in the div. In the app I am working on there is text added to the div based on user actions on the page. The div is there because the users want to see this "audit" generated as they manipulate the values on the page. Then when the form is submitted they want this auto generated audit to be sent up and stored. So, that is why I needed to get the innerHTML out of the div.

I went the hidden input way and it works fine. Thanks for the help all.

Troy
 
Ahh see that there tells me you are already using JS to manipulate the contents of the DIV, this same JS would be able to manipulate a hidden field.
Just as a warning though, people can and sometimes do turn off Javascript in their browsers so just be forewarned, that it may not work in those cases.

Its not too common, but it does happen.

BTW, Sorry if I came off as rude or jerk-like did not mean to.







----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita, no offense taken. Thanks all for the help.

Troy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top