2

Hey guys I want to pass variable pid value to hidden text field pid but unable to do so.

HTML code:

<div id="overlay_forma">
<h4>Another popup</h4>

<input id="addElement" type="button" value="Add to Form" />
<input id="removeElement" type="button" value="Remove" />
<form id="samplecode" name="samplecode" method="POST" action="<%= request.getContextPath() %>/AddProjectTaskDAO">
 <fieldset>
  <legend><b>&nbsp;&nbsp;&nbsp;Dynamically added form elements&nbsp;&nbsp;&nbsp;</b></legend>
  <div id="generatedForm">
   <div>
    <input id="processForm" type="submit" value="Submit to Process" />
   </div>  
  </div>
 </fieldset>
 </form>
 <center><a href="#" id="closea" >Close</a></center>
 </div>

jQuery code:

var counter = 1;

$("#addElement").click(function(event){

 var pid=$('#addElement').data('pid');
 var pname=$('#addElement').data('pname');

 if(counter>10){
     alert("Only 10 textboxes allow");
     return false;
}   

 var $newDiv = $(document.createElement('div'))

  .attr("id", 'TextBoxDiv' + counter);

 var $newInput = $("<label>Input Box: </label>" + "<input type='hidden' id='pid' name='pid' value='pid'>" + "<select name='uname'><c:forEach items='${ual}' var='u'><option value='<c:out value='${u.uname}'></c:out>'><c:out value='${u.uname}'></c:out></option></c:forEach></select>" + "<textarea rows='5' cols='15'></textarea>" + "<input type='text'>");
 $newInput
   .attr("name", "$('#addElement').data('pid')" + counter)
   .addClass("text")
   .attr("name", "uname" + counter)
   .addClass("text")
   .attr("name", "ptdesc_emp" + counter)
   .addClass("text")
   .attr("name", "ptestd_time_alloc" + counter)
   .addClass("text");
 $newInput.appendTo($newDiv);
 $newDiv.appendTo($("#generatedForm"));

 counter++;

 });

I have also tried adding $('#addElement').data('pid') instead of pid in val of hidden text field but still unable to show the output of pid.

Any help is appreciated.

1
  • Ehsan Sajjad updated with html code Commented Jun 4, 2014 at 7:53

1 Answer 1

1

Use '"+pid+"' - pid is variable.

<input type='hidden' id='pid' name='pid' value='"+pid+"'>

and not

<input type='hidden' id='pid' name='pid' value='pid'>

Replace with this line

var $newInput = $("<label>Input Box: </label>" + "<input type='hidden' id='pid' name='pid' value='"+pid+"'>" + "<select name='uname'><c:forEach items='${ual}' var='u'><option value='<c:out value='${u.uname}'></c:out>'><c:out value='${u.uname}'></c:out></option></c:forEach></select>" + "<textarea rows='5' cols='15'></textarea>" + "<input type='text'>");

Note :

In the code you are trying to append id='pid' stays there for every append event. This will result into duplicate IDs.

Working Fiddle.

Sign up to request clarification or add additional context in comments.

3 Comments

shaunakde I replaced pid with "+pid+" it is still not working
working in the fiddle. Inspect the hidden element in console : $('#pid').val()
yup got it working thanks. I made changes over here .attr("name", "$('#addElement').data('pid')" + counter) and also "+pid+"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.