2

This is my form_input:

 <?php 
      echo form_input('noRegistrasi', isset($noRegistrasi) ? 
      $noRegistrasi : '', 'type="text" 
      class="form-control input-sm"   id="noRegistrasi" 
      placeholder="No Registrasi" id="noRegistrasi"'); 
?>

I want it to be dynamic, so user can add more form_input and therefore, I need a Javascript function.

As comparison, this is my Javascript code to generate normal HTML input tag.

function generateSUBKLP(index) {
        var idx = document.createElement("input");
        idx.type = "text";
        idx.name = "SUBKLP" + index + "";
        idx.id = "SUBKLP[" + index + "]";
        idx.size = "10";
        return idx;
    }

My question is: how do I generate codeigniter form_input() using Javascript?

If my question is not clear, please ask.^^

2
  • you can append the html directly. Commented Oct 16, 2015 at 5:14
  • Ans also please explain what and all will be dynamic in that.. Commented Oct 16, 2015 at 5:16

2 Answers 2

1

Here is the demo. Pls load jquery first

Html part:

<form method="post" action="collect_vals.php">
        <div id="input_fields">
            <div><input type="text" name="name[]"> <input type="text" name="project[]"> <span class="fa fa-plus-circle" id="add_field"></span></div>
        </div>
        <input type="submit" value="submit">
</form>

Javascript part:

$(document).ready(function() {
    $("#add_field").click(function(e){
        e.preventDefault();  
            $("#input_fields").append('<div><input type="text" name="name[]"/> <input type="text" name="project[]"> <span id="remove" class="fa fa-minus-circle"></span</div>'); 
    });

    $("#input_fields").on("click","#remove", function(e){ 
        e.preventDefault(); $(this).parent('div').remove();
    })
});
Sign up to request clarification or add additional context in comments.

Comments

1

Also use codeigniter form_input like this

$(document).ready(function() {
    var input = "echo form_input('username', 'johndoe');";
    $('#input_fields').append("<div>"+input+"</div>");
});

Comments

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.