Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Bullet Point to <p> when highlighted dynamically?

Update: Example: <p contenteditable="true">Text Text </p>
Is it possible, within the example, to select the second "text" word within the <p> element and click the button to add a bullet point only to the selected "word" dynamically?


Is it possible, within a <p contenteditable="true">Text</p> to highlight selected text from the <p> element and add a bullet point to the selected text on a button click dynamically?

If an updated fiddle could be provided, it would be very much appreciated, as I am still new to coding.

Fiddle

HTML:

<button>
Apply Bullet Point to Selected Text
</button>

<p contenteditable="true">
Text
Text
</p>

<br>

<p contenteditable="true">
Text
Text
</p>
like image 700
Dave Avatar asked Dec 17 '15 05:12

Dave


People also ask

How to insert bullet point in word using Alt code?

Insert bullet point in Word using Alt Code (Shortcut) 1 On the Insert tab, click Symbol and then click More Symbols…. The Symbol dialog appears. 2 Locate the Bullet Point symbol, then click to select it.

Can you put a bullet point in a column?

In this case, you can put bullets in a separate column, align them right, and remove the border between the two columns. How to add bullet points in Excel using Symbol menu If you don't have a number pad or forget a key combination, here's another quick easy way to insert bullet in Excel: Select a cell where you want to add a bullet point.

How do I make a list into a bullet point?

Here's how: Go to the Insert tab, Text group, and click the Text Box button: In the worksheet, click where you want to have the text box and drag it to the desired size. Type the list items in the text box. Select the lines you want to turn into bullet points, right-click on them, and then click the little arrow next to Bullets:

How to insert bullet points in Microsoft Excel 365?

Select the cell where you want to put a bullet point. On the Home tab, in the Font group, change font to Wingdings. Type a small "l" letter to insert a filled circle bullet (●) or "n" to add a square bullet point (■) or some other letter shown in the screenshot below: You can insert even more bullet symbols by using the CHAR function.


4 Answers

You can do it the following way using jQuery:

function getText() {
    if (window.getSelection) {
        return window.getSelection().anchorNode.parentNode;
    } 
    return '';
}

$(document).ready(function () {
    $('#btnClick').click(function () {
        getText().innerHTML = "\u2022" + getText().innerHTML;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnClick">
Apply Bullet Point to Selected Text
</button>

<p id="text1" contenteditable="true">
Text
Text
</p>

<br>

<p id="text2" contenteditable="true">
Text
Text
</p>

Fiddle 1


If you want to replace the selected text with a bullet then do the following:

getText().anchorNode.parentNode.innerHTML = "\u2022" + getText();   

Otherwise if you want to append/edit the text, then you want to put more effort into it:

Fiddle 2

Updated:

At the end get the result:

$(document).ready(function () {
    $('#btnClick').click(function () {

    var selected = getText();
    if (selected.toString().length > 0) {
      if ($(selected.anchorNode.parentNode).attr('contenteditable')) {
        var bulletText = document.createTextNode(" \u2022 " + getText() );
        selected.getRangeAt(0).surroundContents(bulletText);
      }
    }
    });
});

Working Fiddle

like image 163
ketan Avatar answered Oct 19 '22 04:10

ketan


Yes. It is possible. Using a similar example from this answer, you can consider using the following JS. The logic is to wrap the selected text with <li> element.

$('button').on("click", function(e) {
  var selected = getSelection();
  var range = selected.getRangeAt(0);
  if (selected.toString().length > 0) {
    // Check if parent element has contenteditable attr set to true.
    if ($(selected.anchorNode.parentNode).attr('contenteditable')) {
      var newNode = document.createElement("li");
      range.surroundContents(newNode);
    }
  }
  selected.removeAllRanges();
});

function getSelectedText() {
  var selectedText = '';
  if (window.getSelection) {

    selectedText = window.getSelection();
  } else if (document.getSelection) {
    selectedText = document.getSelection();
  } else if (document.selection) {
    // For IE prior to 9 as it does not support document.getSelection().
    selectedText = document.selection.createRange().text;
  } else return;
  return selectedText;
}

See this fiddle for a working example.


Edit

I've also updated the code such that elements without contenteditable set to true won't have the <li> element wrapped.

Updated fiddle and code (see above). This should be what you want.

like image 30
Jia Jian Goi Avatar answered Oct 19 '22 03:10

Jia Jian Goi


Here the simple solution will work fine.

$(document).ready(function(){

   var lstEl;
  
  $("p").on('focus', function () {
       lstEl = $(this);
  });
  
  $("button").click(function(){
    if(!lstEl.hasClass("bult")){
      lstEl.addClass('bult')
      }
  })
})
.bult{
     list-style:disc outside none;
     display:list-item; 
  margin-left:10px
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button>
Apply Bullet Point to Selected Text
</button>

<p contenteditable="true">
Text
Text
</p>

<br>

<p contenteditable="true">
Text
Text
</p>
like image 1
Shailendra Sharma Avatar answered Oct 19 '22 03:10

Shailendra Sharma


you can use jquery plugin.

$(function(){
        	$('button').on('click', function(){
          $('p').css({'display': 'list-item', 'margin-left':'25px'});
          
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
Apply Bullet Point to Selected Text
</button>

<p contenteditable="true">
Text
Text
</p>

<br>

<p contenteditable="true">
Text
Text
</p>
like image 1
Anshuk Avatar answered Oct 19 '22 02:10

Anshuk