How to impose maxlength on textArea in HTML using JavaScript

window.onload = function() { var txts = document.getElementsByTagName(‘TEXTAREA’); for(var i = 0, l = txts.length; i < l; i++) { if(/^[0-9]+$/.test(txts[i].getAttribute(“maxlength”))) { var func = function() { var len = parseInt(this.getAttribute(“maxlength”), 10); if(this.value.length > len) { alert(‘Maximum length exceeded: ‘ + len); this.value = this.value.substr(0, len); return false; } } txts[i].onkeyup = func; txts[i].onblur = … Read more

How to get selected text from a textbox control with JavaScript

OK, here is the code I have: function ShowSelection() { var textComponent = document.getElementById(‘Editor’); var selectedText; if (textComponent.selectionStart !== undefined) { // Standards-compliant version var startPos = textComponent.selectionStart; var endPos = textComponent.selectionEnd; selectedText = textComponent.value.substring(startPos, endPos); } else if (document.selection !== undefined) { // Internet Explorer version textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; … Read more

Rendering HTML inside textarea

This is not possible to do with a textarea. What you are looking for is an content editable div, which is very easily done: <div contenteditable=”true”></div> jsFiddle div.editable { width: 300px; height: 200px; border: 1px solid #ccc; padding: 5px; } strong { font-weight: bold; } <div contenteditable=”true”>This is the first line.<br> See, how the text … Read more

How to get the caret column (not pixels) position in a textarea, in characters, from the start?

With Firefox, Safari (and other Gecko based browsers) you can easily use textarea.selectionStart, but for IE that doesn’t work, so you will have to do something like this: function getCaret(node) { if (node.selectionStart) { return node.selectionStart; } else if (!document.selection) { return 0; } var c = “\001”, sel = document.selection.createRange(), dul = sel.duplicate(), len … Read more

Using PHP read word in textarea line by line and insert to mySQL

you can code like: $val=”Veg Tomato Potato Fruits mango banana”; $textAreaVal = $val; $arrVal = explode(PHP_EOL, $textAreaVal); for($i=0; $i < count($arrVal); $i++){ $type=””; $isInIf = false; if($arrVal[i] == ‘Veg’){ $type=”Veg”; }else if($arrVal[i] == ‘Fruits’){ $type=”Fruits”; } if($type != ”){ // your insert query here // insert into tbl(type, name) values(‘$type’, ‘$arrVal[i]’) } }