JiveMedia
asked on
Cloning User Inputs
Hi,
Looking for some help with a booking form that i have already created (which is working fine).
The booking form currently has a 'select' dropdown with a number range of 1-10 (number of learners for the course).
When the number is changed in the user select, depending on the number chosen, the below html div gets cloned x number selected and the input name changes (number incremented on the end of name field).
Example of generated HTML if '2' is selected in dropdown.
The current JS code that works for this as below (First couple lines can be ignored).
I was looking to add three inputs in as below and have them all cloned like above but now with a incrementing input id and different names for each input.
So the output generated would be something like this if '2' was selected:
All help would be greatly appreciated.
Thanks!
Looking for some help with a booking form that i have already created (which is working fine).
The booking form currently has a 'select' dropdown with a number range of 1-10 (number of learners for the course).
<div class="user_bf_input">
<label for="course_qty">Number of Learners:</label>
<select name="course_qty" id="course_qty" class="course_qty">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
When the number is changed in the user select, depending on the number chosen, the below html div gets cloned x number selected and the input name changes (number incremented on the end of name field).
<div class="user_bf_input">
<label>*Learners Full Name:</label>
<input type="text" name="pname1" size="30" class="required" />
</div>
Example of generated HTML if '2' is selected in dropdown.
<div class="user_bf_input">
<label>*Learners Full Name:</label>
<input type="text" name="pname1" size="30" class="required" />
</div>
<div class="user_bf_input">
<label>*Learners Full Name:</label>
<input type="text" name="pname2" size="30" class="required" />
</div>
The current JS code that works for this as below (First couple lines can be ignored).
$("#course_qty").change(function() {
var price = parseFloat($('#course_cost').val(), 10);
var qty = (parseInt($('#course_qty').val(), 10) || 0);
var total = (price*qty).toFixed(2);
$("#course_total").val(total);
var x = $('#course_qty').val();
$('#quantity').val(x);
input = $('.learner_input .user_bf_input').eq(0).clone();
$('input', input).val('');
$('.learnerNames').val('');
$('.learner_input .input_area').empty();
for (var i=1; i<=$(this).val(); i++) {
$('input', input).attr('name', 'pname'+i);
$(input).clone().appendTo(".learner_input .input_area");
};
});
I was looking to add three inputs in as below and have them all cloned like above but now with a incrementing input id and different names for each input.
<div class="user_bf_input">
<label>*Learners Full Name:</label>
<input id="learnerName-" name="pname1" type="text" class="required learnerInputBox" placeholder="First Middle Last" size="24" />
<input id="learnerUSI-" name="pusi1" type="text" class="required learnerInputBox" placeholder="USI Number" size="24" />
<input id="learnerEmail-" name="pemail1" type="text" class="required learnerInputBox" placeholder="learner@example.com" size="24" />
</div>
So the output generated would be something like this if '2' was selected:
<div class="user_bf_input">
<label>*Learners Full Name:</label>
<input id="learnerName-1" name="pname1" type="text" class="required learnerInputBox" placeholder="First Middle Last" size="24" />
<input id="learnerUSI-1" name="pusi1" type="text" class="required learnerInputBox" placeholder="USI Number" size="24" />
<input id="learnerEmail-1" name="pemail1" type="text" class="required learnerInputBox" placeholder="learner@example.com" size="24" />
</div>
<div class="user_bf_input">
<label>*Learners Full Name:</label>
<input id="learnerName-2" name="pname2" type="text" class="required learnerInputBox" placeholder="First Middle Last" size="24" />
<input id="learnerUSI-2" name="pusi2" type="text" class="required learnerInputBox" placeholder="USI Number" size="24" />
<input id="learnerEmail-2" name="pemail2" type="text" class="required learnerInputBox" placeholder="learner@example.com" size="24" />
</div>
All help would be greatly appreciated.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
provide a link to your website
ASKER
https://nationwidetraining .com.au/on line_booki ng_new/
Change the number under 'Number of Learners' and the div's should appear under the Learners Information title.
Change the number under 'Number of Learners' and the div's should appear under the Learners Information title.
remove theses lines :
input = $('.learner_input .user_bf_input').eq(0).clone();
$('input', input).val('');
$('.learnerNames').val('');
ASKER
Hi leakim971,
Still no luck after removing the lines!
Still no luck after removing the lines!
the following should work :
$("#course_qty").change(function() {
var price = parseFloat($('#course_cost').val(), 10);
var qty = (parseInt($('#course_qty').val(), 10) || 0);
var total = (price*qty).toFixed(2);
$("#course_total").val(total);
var x = $('#course_qty').val();
$('#quantity').val(x);
var sets = $('.learner_input .user_bf_input:gt(0)');
var how_many_presents = sets.length;
var how_many_to_display = $(this).val();
if(how_many_to_display>how_many_presents) {
for(var f=1; f<=$(this).val(); f++) {
var input = $('.learner_input .user_bf_input:first').clone();
$(':text', input).val('');
$(':text', input).each(function(i, v) {
var id = $(this).attr("id").replace(/\d/,"") + f; // maybe you want "i" instead "(i + 1"
var name = $(this).attr("name").replace(/\d/,"") + f; // maybe you want "i" instead "(i + 1"
$(this).attr({"id": id, name:name });
});
input.appendTo(".learner_input .input_area:last");
}
}
else if(how_many_to_display<how_many_presents) {
$('.learner_input .user_bf_input:gt(' + (1+how_many_to_display*1) + ')').remove();
}
});
ASKER
Hi Leakim971,
Still no luck, am i suppose to be changing any values in your code?
Still no luck, am i suppose to be changing any values in your code?
I don't see the latest code on your website
ASKER
Its definitely uploaded and visible for me, please see the attached screenshot!
Screen-Shot-2014-12-02-at-7.51.53-am.png
Screen-Shot-2014-12-02-at-7.51.53-am.png
it work fine, I see you're hiding the field with class "hide" so you can't see them
ASKER
The hide class is only applied to divs that are not selected in the 'Payment Method' dropdown! If you choose 'Money Order' for example, that information block gets style="display:block" applied. Its not working otherwise their would be 10 rows of input boxes under 'Learners Information' if i selected 10 from the dropdown at the top!
create a div to put the input boxes with a dedicated id, for example id="boxes"
and replace : input.appendTo(".learner_i nput .input_area:last");
by : input.appendTo("#boxes");
and replace : input.appendTo(".learner_i
by : input.appendTo("#boxes");
ASKER
I have added a div with the ID 'boxes' and it is now displaying multiple rows when a different value is selected in the dropdown. Doesn't seem to be functioning correctly however. If i choose '10' then change it back to '2', the rows aren't removed. Also, if i choose '3' in the select for example, 4 rows are displayed when its suppose to be 3.
replace everywhere : how_many_to_display<how_ma ny_present s
by : how_many_to_display*1<how_ many_prese nts
by : how_many_to_display*1<how_
ASKER
Still no luck.
replace everywhere : how_many_to_display>how_ma ny_present s
by : how_many_to_display*1>how_ many_prese nts
by : how_many_to_display*1>how_
ASKER
I've requested that this question be deleted for the following reason:
Solved it through another website.
Solved it through another website.
Hi JiveMedia,
Just deleting questions like this opens the EE system to abuse, especially when you haven't even given feedback on the last suggestion from leakim971, which means he didn't have a chance to understand/fix any outstanding issues. leakim971 put considerable work into this, most of which was probably quite valid, and seemed to basically work at the http://jsfiddle.net/prmd5dfu/ link where he demonstrated it. The remaining problem seemed to be that you were not able to take his code and get it to work in your environment, which may be beyond the scope of the original question.
Apart from that, you have not even shown us the solution you got from the other site, which opens the EE system to abuse.
I would suggest that you give leakim971 an A grade for the code he supplied at jsfiddle.net.
Or have I missed something?
Just deleting questions like this opens the EE system to abuse, especially when you haven't even given feedback on the last suggestion from leakim971, which means he didn't have a chance to understand/fix any outstanding issues. leakim971 put considerable work into this, most of which was probably quite valid, and seemed to basically work at the http://jsfiddle.net/prmd5dfu/ link where he demonstrated it. The remaining problem seemed to be that you were not able to take his code and get it to work in your environment, which may be beyond the scope of the original question.
Apart from that, you have not even shown us the solution you got from the other site, which opens the EE system to abuse.
I would suggest that you give leakim971 an A grade for the code he supplied at jsfiddle.net.
Or have I missed something?
ASKER
I have accepted leakim971's suggestions as his jsfiddle works.
I needed to amend it in other ways to make it work however as it wasn't completely right on my end.
Thanks for all your help and time on this!
I needed to amend it in other ways to make it work however as it wasn't completely right on my end.
Thanks for all your help and time on this!
ASKER
Thanks for posting your reply.
I have tried implementing your above code but it isn't working.
I have attached the new booking form file, could you please look over?
Any ideas?
Many thanks.
booking-form2.js