I am trying to append some digits to the class names of the elements I am trying to dynamically add on a page. The digit that I will append is by getting the number of the existing target element on the page and I am getting it through the jquery function length.
Here is my code:
$("#add_ingr").click(function(){
var id = $('.save').length + 1;
var row = '<tr>'
+'<td>'
+'<?php $data = array('name' => 'ingr_name[]', 'class' => 'ingr_name'); echo form_input($data); ?>'
+'</td>'
+'<td>'
+'<?php $data = array('name' => 'ingr_amount[]', 'class' => 'amt'); echo form_input($data); ?>'
+'</td>'
+'<td>'
+'<?php $data = array('name' => 'ingr_unit[]', 'class' => 'unit'); echo form_input($data); ?>'
+'<span class="remove">X</span>'
+'<span class="save" id="' +count +'">Save</span>'
+'</td>';
+'</tr>';
$("#ingr_table > tbody").append(row);
});
I’m having troubles because the var id is of javascript and I wanted to append it in a php array value but I am not successfully running the code because it generates errors when I do something like this:
$("#add_ingr").click(function(){
var id = $('.save').length + 1;
var row = '<tr>'
+'<td>'
+'<?php $data = array('name' => 'ingr_name[]', 'class' => 'ingr_name_' +id); echo form_input($data); ?>'
+'</td>'
+'<td>'
+'<?php $data = array('name' => 'ingr_amount[]', 'class' => 'amt_') +id; echo form_input($data); ?>'
+'</td>'
+'<td>'
+'<?php $data = array('name' => 'ingr_unit[]', 'class' => 'unit_' +id); echo form_input($data); ?>'
+'<span class="remove">X</span>'
+'<span class="save" id="' +count +'">Save</span>'
+'</td>';
+'</tr>';
$("#ingr_table > tbody").append(row);
});
Does anybody have a solution for my problem? Thank you very much for those who would help.
Do you really need CodeIgniter’s
form_inputhelper?As this will just return
<input name=$name class=$class/>,I guess you can convert it to this.