Is there a way to add bullet points to an HTML textarea?
I want to add a simple feature where a bullet point is added for every line in a text area (similar to a list bullet points).
Set BULLET to whatever character code you prefer. This works great, just remove the redundant oninput="" attribute from the textarea html, otherwise you'll be calling it twice.
Type* and a space before your text, and Word will make a bulleted list. To complete your list, press Enter until the bullets or numbering switch off.
You add the STYLE attribute within the <ul> start tag. This tag makes the items surrounded by the line item tags <li>… </li> appear as a list with square bullets.
You can't do that, but there is another way. Delete the textarea:
'<section id="textarea" contenteditable="true">
<ul>
<li>List item here</li>
<li>List item here</li>
<li>List item here</li>
<li>List item here</li>
</ul>
</section>'
I think you will not able to add bullet points (i.e., rich text) in a textarea (HTML form) or any other HTML input control.
But you may achieve the result by adding a rich-text editor.
Or you need to write jQuery code to handle the event and display the result, like if the user is simply viewing content, then show that in HTML and CSS format and if the user clicks on the content then show text area and allow to add more text.
<textarea id="banner-message" class="message" style="display:none">
</textarea>
<div id="display" class="message" style="overflow-y:auto">
</div>
var strings = [];
strings.push(
"first text",
"second text",
"third text"
);
var htmlContent = '';
var textAreaContent = '';
$(document).ready(function() {
strings.forEach(element => htmlContent += "<li>" + element + "</li>");
$("#display").html(htmlContent);
var i = 1;
strings.forEach(function(element) {
if(strings.length == i)
textAreaContent += ">" + element;
else
textAreaContent += ">" + element + "\n";
i++;
});
$("#banner-message").val(textAreaContent);
})
$("#display").click(function() {
$(this).css("display", "none");
$("#banner-message").css("display", "");
var currentText = $("#banner-message").val();
//currentText += "\n>";
$("#banner-message").val(currentText);
$("#banner-message").focus();
});
$("#banner-message").blur(function() {
var currentText = $("#banner-message").val();
var plainText = currentText.replace(/>/g, "")
var splitText = plainText.split("\n");
console.log(splitText);
htmlContent = '';
splitText.forEach(element => htmlContent += "<li>" + element + "</li>");
$("#display").html(htmlContent);
$(this).css("display", "none");
$("#display").css("display", "");
})
$("#banner-message").keyup(function(e) {
var code = e.keyCode ? e.keyCode : e.which;
if (code == 13) {
var text = $(this).val();
text += ">";
$(this).val(text);
}
});
Here is a demo: https://jsfiddle.net/v5unL369/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With