Log in

View Full Version : how to pass div values to form



mutago
06-02-2014, 09:04 PM
i have a div values that i want to hide and then pass its value into form
I tried this below. any help





<div id="records" type="hidden">
<input type="text" name="records" value="records">
</div>

Beverleyh
06-02-2014, 10:56 PM
Unfortunately you can't pass div values (there isn't such a thing) - you'd pass the input as a hidden field instead;
<input type="hidden" name="records" value="records">Here you are only passing the word "records" though, which may be correct, but it's also likely that you'd want to pass a dynamic variable via php for example;
<input type="hidden" name="records" value="<?php echo $records;?>">

jscheuer1
06-12-2014, 03:19 AM
Element div cannot technically have value or type properties. And you cannot pass the value of any of its valid properties without javascript or perhaps server side code (only by copying). You have posted (or someone else has moved the post) to the HTML section, so it's hard to understand what language you intend to do this in. Could you be more specific about what you're trying to accomplish and using what language?

Praveena
07-04-2014, 08:48 AM
If you have the need to submit content rendered inside a <div> element along with other form elements try this.
For every <div> a <textarea> is created and filled with the inner HTML of the div. The name attribute for the <div> is id of the form+'div'. The value of the id attribute of the above mentioned <div> will be used as the name attribute of the <textarea>




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form id="form" name="formname" action="index.html" method="get" onsubmit="div2form('form')">
<input id="i1" name="i1name" type="text" value="10">
<div id=t1 name="formdiv">sdajda;lkdja;ljdal;dj</div>
<div id=t2 name="formdiv">aaaaaaaaaaaaaaaaaaaaaa</div>
<input type="submit" name="submit" value="submit">
</form>
</body>
<script type="text/javascript" >
function div2form(id){
var form=document.getElementById(id);
if(!form){
return;
}
var divs=document.getElementsByName(id+'div')
var i, ndivs=divs.length;
for(i=0;i<ndivs;i++){
if(document.getElementById('textarea'+divs[i].id)){
document.getElementById('textarea'+divs[i].id).value=divs[i].innerHTML;
} else {
var texta=document.createElement('TEXTAREA');
texta.name=divs[i].id;
texta.id='textarea'+divs[i].id;
texta.value=divs[i].innerHTML;
texta.style.display='none';
form.appendChild(texta);
}
}
}
</script>