I'm using Wordpress and am creating a frontend form where users can upload pictures. I have implemented an "Add More" Button, which creates another input field.
However, the Button Link (Select Image) on the "added" fields does not trigger the Media Uploader window. The first button works fine, however.
Here's my code
frontend-form.php
..
<p class="text-box">
<a class="add-box btn" href="#">Add More</a>
<input type="text" size="36" name="image_1" value="http://" />
<input id="image_1" class="button upload_image_button" type="button" value="Upload Image" />
</p>
media-upload.js
jQuery(document).ready(function($){
var custom_uploader;
$('.upload_image_button').click(function(e) {
var target_input = $(this).attr('id');
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: false
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.on('select', function() {
attachment = custom_uploader.state().get('selection').first().toJSON();
$('input[name=' + target_input + ']').val(attachment.url);
});
//Open the uploader dialog
custom_uploader.open();
});
});
addmore.js
jQuery(document).ready(function($){
$('.item-submit .add-box').click(function(){
var n = $('.text-box').length + 1;
if( 8 < n ) {
alert('Maximum number of images reached!');
return false;
}
var box_html = $('<p class="text-box"><input type="text" size="36" name="image_' + n + '" value="http://" /><input id="image_' + n +'" class="button upload_image_button" type="button" value="Upload Image" /> <a href="#" class="remove-box">Remove</a></p>');
box_html.hide();
$('.item-submit p.text-box:last').after(box_html);
box_html.fadeIn('slow');
return false;
});
$('.item-submit').on('click', '.remove-box', function(){
$(this).parent().css( 'background-color', '#FF6C6C' );
$(this).parent().fadeOut("slow", function() {
$(this).remove();
$('.box-number').each(function(index){
$(this).text( index + 1 );
});
});
return false;
});
});
All .js scripts are properly enqueued, which I checked using var_dump(wp_print_scripts());
Thank you for your help!