I have a code, that dynamically append form field groups in a page using the underscore
framework and I have a problem with getting the input fields of the template, so I don't know if I am doing something wrong that I cannot see.
The underscore
template is like the following:
<script type="text/template" id="redirection_url_element">
<tr>
<td>
<input type="text" class="regular-text" placeholder="<?php esc_attr_e( 'Enter the redirection URL', 'WPL' ); ?>" value="<%= (typeof( redirection_url ) !== 'undefined') ? redirection_url : '' %>" data-name="redirection_url" data-group="redirection" />
</td>
<td>
<input type="text" class="regular-text" placeholder="<?php esc_attr_e( 'Weight', 'WPL' ); ?>" value="<%= (typeof( redirection_weight ) !== 'undefined') ? redirection_weight : '' %>" data-name="redirection_weight" data-group="redirection" />
</td>
<td>
<i class="remove-parent-row">×</i>
</td>
</tr>
</script>
So in my javascript code I do the following:
console.log ( $ ( '#redirection_url_element' ).find( ':input' ) );
and I get the following result:
prevObject: e.fn.init[1], context: document, selector: "#redirection_url_element :input"]
context: document
length: 0
prevObject: e.fn.init[1]
selector: "#redirection_url_element :input"
__proto__: m[0]
and it seem that it cannot find the input fields. If you see the length is 0
.
Do you think I am doing something wrong ? Is there any error that I cannot see ?
UPDATE #1
The way I use the template is like following:
var $template_result = _.template ( $ ( '#redirection_url_element' ).html (), $data )
This is how I translate the template into HTML on the fly and I don't use the backbone
. I just inject the $template_result
into the destination element.
UPDATE #2
So, @Wolff, the table looks like that:
<table class="repeater_table">
<tbody id="redirection_url_container">
<tr>
<td>
<input type="text" class="regular-text" placeholder="Enter the redirection URL" value="" data-name="redirection_url" data-group="redirection">
</td>
<td>
<input type="text" class="regular-text" placeholder="Weight" value="" data-name="redirection_weight" data-group="redirection">
</td>
<td>
<i class="remove-parent-row">×</i>
</td>
</tr>
</tbody>
</table>
UPDATE #3
Ok, I found the solution thanks to @Marcos Pérez Gude note and it is like following:
console.log ( $( $.parseHTML( $ ( '#redirection_url_element' ).text() ) ).find( ':input' ) );
I know it is somehow ugly, but it does the work for me. Do you think is there any better way to achive the above ?