NarulaKeshav NarulaKeshav - 1 year ago 60
jQuery Question

Changing "+" in textarea to <input type='checkbox'>

I want to convert all the

sign in the textarea field where the user types and parse that
sign and the text after it until the newline character into an HTML
input checkbox
For example, if the user, in the
, types:

+ Feed the kittens
+ Call the doctor
+ Go buy grocery

I want to parse it to something like:

<input type="checkbox"><label>Feed the kittens</label>
<input type="checkbox"><label>Call the doctor</label>
<input type="checkbox"><label>Go buy grocery</label>

I need to return this as a
with the
in it.

I created a
that takes in
which is the text in the
. How should I go about writing this?

Answer Source

you may refer below code for an idea:

$(document).ready(function() {
  $("#parseButton").click(function() {
    var str = $("#mytext").val(); //get the text entered by user

    var allLabels = str.split("+"); //split the text assuming that user will be writing data in required fashion

    allLabels.shift(); //skip the 0th index of array which will be blank

    var htmlString = "";
    //iterate over all the tasks one by one to form html string
    $.each(allLabels, function(index, value) {

      htmlString += '<input type="checkbox"><label>' + value + '</label>';

    //append the html to div

<script src=""></script>
<textarea id="mytext" rows="4" cols="50">
  + Feed the kittens 
  + Call the doctor 
  + Go buy grocery</textarea>
<button id="parseButton">Parse</button>

<div id="myHtml"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download