Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert paragraph or new elements into checkbox

I have contenteditable div as text editor

Functionality needed is whatever the user writes or copy/paste in text editor turns into checkbox

for eg

Case 1

If User writes the following

Test1(enter key pressed)

Test2(enter key pressed)

Test3(enter key pressed)

then there should be three checkbox for Test1,Test2,Test3 respectively

And

Case2

when the user writes

Parent1 (enter key pressed)

  • Test1(enter key pressed)

  • Test2(enter key pressed)

  • Test3(enter key pressed)

In the above case there will be 4 checkbox parent1 and 3 li's

Things i have achieved but i am open to accept new way if more accurate.

$('#checkbox').click(function() {
var $p = "";  
     var re = /\S/;
     $p = $('.outputBox');
      $p.contents()
      .filter(function() {    
        return this.nodeType == 3&& re.test(this.nodeValue);;  
     })
    .wrap('<label> <input type="checkbox" name="field_1" value="on" />')
});

FIDDLE Demo

Basically a new span,div,br tags or for that matter li's should be created into checkboxes.

If there is any different method which will fulfill both the requirement/basic requirement of creating checkboxes it is most welcome.

like image 594
Vaibs_Cool Avatar asked Jun 12 '15 09:06

Vaibs_Cool


People also ask

How do you create a checkbox for text in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!

Which element is used to create a checkbox?

Checkboxes are created with the HTML <input> tag. It can be nested inside a <form> element or they can stand alone. They can also be associated with a form with the help of form attribute of the <input> tag.


2 Answers

Here is a possible solution. I have made a traversal to the innermost text element and then convert that into a checkbox.

$('#checkbox').click(function() {
     var $p = "";  
     $p = $('.outputBox');
     wrapText($p);
});

function wrapText(domObj) {
    var re = /\S/;
    domObj.contents().filter(function() {
        if (this.nodeType === 1) {
            wrapText($(this));
        } else {
            return (this.nodeType === 3 && re.test(this.nodeValue));
         }
    })
    .wrap('<label> <input type="checkbox" name="field_1" value="on" /></label>');
}

Here is the fiddle: http://jsfiddle.net/wLrfqvss/6/

Hope thats what you are looking for.

If you want this to happen for only certain elements, you could always add a check like so:

if (this.nodeType === 1 && this.tagName === 'DIV')

in the above code.

like image 126
Arathi Sreekumar Avatar answered Oct 11 '22 18:10

Arathi Sreekumar


you can use template for checkbox instead of hardcoding html

<div id="template" style="visibility:hidden">
<label> <input type="checkbox" name="field_1" value="on" /></label>
</div>

in code

$('#checkbox').click(function() {});

 function createElement()
{
 var targetElement=$("#template").clone(true);
 targetElement.removeAttr("style");
 targetElement.attr("name","new id");
 var $p = "";  
 var re = /\S/;
 $p = $('.outputBox');
  $p.contents()
  .filter(function() {    
    return this.nodeType == 3&& re.test(this.nodeValue);;  
 })
.wrap(targetElement)
}

this will also provide you flexibility of using any html template as the code is not control specific

like image 40
Akshita Avatar answered Oct 11 '22 19:10

Akshita