I have some premade javascript, that interacts with a form for canned responses from my database. When clicking the insert button nothing happens though and chrome is showing the error: Uncaught TypeError: Cannot read property 'selectionStart' of null
Here is the javascript/php/html:
<tr><?php echo tep_draw_form('status', FILENAME_ORDERS, tep_get_all_get_params(array('action')) . 'action=update_order'); ?>
<td class="main">
<script language="javascript">
function insereTexto(){
//Paste into textarea
var textarea = document.getElementById("txt");
//Text to be inserted
var texto = document.getElementById("txtValor").value;
//beginning of the selection
var sel_start = textarea.selectionStart;
//end of the selection
var sel_end = textarea.selectionEnd;
if (!isNaN(textarea.selectionStart))
//Treatment for Mozilla
{
var sel_start = textarea.selectionStart;
var sel_end = textarea.selectionEnd;
mozWrap(textarea, texto, '')
textarea.selectionStart = sel_start + texto.length;
textarea.selectionEnd = sel_end + texto.length;
}
else if (textarea.createTextRange && textarea.caretPos)
{
if (baseHeight != textarea.caretPos.boundingHeight)
{
textarea.focus();
storeCaret(textarea);
}
var caret_pos = textarea.caretPos;
caret_pos.text = caret_pos.texto.charAt(caret_pos.texto.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
}
else //For those who can not enter, insert at the end of it (ie. ..)
{
textarea.value = textarea.value + texto;
}
}
/*
This function opens the text of two strings and inserts the text at the cursor position well after he joins novamento text but the text entered
This wonderful feature only works in Mozilla ... IE does not have the properties SelectionStart, textLength ...
*/
function mozWrap(txtarea, open, close)
{
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
var scrollTop = txtarea.scrollTop;
if (selEnd == 1 || selEnd == 2)
{
selEnd = selLength;
}
//S1 has the text from the beginning to the cursor position
var s1 = (txtarea.value).substring(0,selStart);
//S2 has the selected text
var s2 = (txtarea.value).substring(selStart, selEnd)
//S3 has all the selected text
var s3 = (txtarea.value).substring(selEnd, selLength);
//Places the text in the textarea. Uses the string that was in the beginning, in the middle of the input string, then the selection string then the
//closing and finally what's left after the selection
txtarea.value = s1 + open + s2 + close + s3;
txtarea.selectionStart = selEnd + open.length + close.length;
txtarea.selectionEnd = txtarea.selectionStart;
txtarea.focus();
txtarea.scrollTop = scrollTop;
return;
}
/*
Insert at Caret position. Code from
[url].phtml/aid/1052/fid/130[/url]
*/
function storeCaret(textEl)
{
if (textEl.createTextRange)
{
textEl.caretPos = document.selection.createRange().duplicate();
}
}
</script>
<select id="txtValor" />
<option value="">Select Text</option>
<option value="<?php echo $order->customer['name']; ?>">Customer Name</option>
<option value="<?php echo STORE_NAME_ADDRESS; ?>">signature</option>
<option value="<?php echo STORE_NAME; ?>">Store Name</option>
<option value="<?php echo $_SERVER['SERVER_NAME']; ?>">Site</option>
<option value="<?php echo STORE_OWNER; ?>">Store Owner</option>
<option value="<?php echo STORE_OWNER_EMAIL_ADDRESS; ?>">Email the Store</option>
<?php
$get_premades_query = tep_db_query("select * from orders_premade_ments order by id");
while($result = mysql_fetch_array($get_premades_query)) {
echo '<option value="'.$result["text"].'">'. $result["title"].'</option>';
}
?>
</select>
<input type="button" value="Insert at the cursor position" onclick="insereTexto()"/>
<input type="button" value="Clean" onclick="this.form.elements['ments'].value=''">
<font class="smallText"><a href="premade_ments.php">[Setup]</a></font><br>
<?php echo tep_draw_textarea_field('ments', 'soft', '80', '10','','id="txt"'); ?></td>
</tr>
I really don't know much javascript, THe php is calling the correct database fields and I can echo them correctly. But I a guessing selectionStart function is not being used correctly?
I have some premade javascript, that interacts with a form for canned responses from my database. When clicking the insert button nothing happens though and chrome is showing the error: Uncaught TypeError: Cannot read property 'selectionStart' of null
Here is the javascript/php/html:
<tr><?php echo tep_draw_form('status', FILENAME_ORDERS, tep_get_all_get_params(array('action')) . 'action=update_order'); ?>
<td class="main">
<script language="javascript">
function insereTexto(){
//Paste into textarea
var textarea = document.getElementById("txt");
//Text to be inserted
var texto = document.getElementById("txtValor").value;
//beginning of the selection
var sel_start = textarea.selectionStart;
//end of the selection
var sel_end = textarea.selectionEnd;
if (!isNaN(textarea.selectionStart))
//Treatment for Mozilla
{
var sel_start = textarea.selectionStart;
var sel_end = textarea.selectionEnd;
mozWrap(textarea, texto, '')
textarea.selectionStart = sel_start + texto.length;
textarea.selectionEnd = sel_end + texto.length;
}
else if (textarea.createTextRange && textarea.caretPos)
{
if (baseHeight != textarea.caretPos.boundingHeight)
{
textarea.focus();
storeCaret(textarea);
}
var caret_pos = textarea.caretPos;
caret_pos.text = caret_pos.texto.charAt(caret_pos.texto.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
}
else //For those who can not enter, insert at the end of it (ie. ..)
{
textarea.value = textarea.value + texto;
}
}
/*
This function opens the text of two strings and inserts the text at the cursor position well after he joins novamento text but the text entered
This wonderful feature only works in Mozilla ... IE does not have the properties SelectionStart, textLength ...
*/
function mozWrap(txtarea, open, close)
{
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
var scrollTop = txtarea.scrollTop;
if (selEnd == 1 || selEnd == 2)
{
selEnd = selLength;
}
//S1 has the text from the beginning to the cursor position
var s1 = (txtarea.value).substring(0,selStart);
//S2 has the selected text
var s2 = (txtarea.value).substring(selStart, selEnd)
//S3 has all the selected text
var s3 = (txtarea.value).substring(selEnd, selLength);
//Places the text in the textarea. Uses the string that was in the beginning, in the middle of the input string, then the selection string then the
//closing and finally what's left after the selection
txtarea.value = s1 + open + s2 + close + s3;
txtarea.selectionStart = selEnd + open.length + close.length;
txtarea.selectionEnd = txtarea.selectionStart;
txtarea.focus();
txtarea.scrollTop = scrollTop;
return;
}
/*
Insert at Caret position. Code from
[url]http://www.faqts./knowledge_base/view.phtml/aid/1052/fid/130[/url]
*/
function storeCaret(textEl)
{
if (textEl.createTextRange)
{
textEl.caretPos = document.selection.createRange().duplicate();
}
}
</script>
<select id="txtValor" />
<option value="">Select Text</option>
<option value="<?php echo $order->customer['name']; ?>">Customer Name</option>
<option value="<?php echo STORE_NAME_ADDRESS; ?>">signature</option>
<option value="<?php echo STORE_NAME; ?>">Store Name</option>
<option value="<?php echo $_SERVER['SERVER_NAME']; ?>">Site</option>
<option value="<?php echo STORE_OWNER; ?>">Store Owner</option>
<option value="<?php echo STORE_OWNER_EMAIL_ADDRESS; ?>">Email the Store</option>
<?php
$get_premades_query = tep_db_query("select * from orders_premade_ments order by id");
while($result = mysql_fetch_array($get_premades_query)) {
echo '<option value="'.$result["text"].'">'. $result["title"].'</option>';
}
?>
</select>
<input type="button" value="Insert at the cursor position" onclick="insereTexto()"/>
<input type="button" value="Clean" onclick="this.form.elements['ments'].value=''">
<font class="smallText"><a href="premade_ments.php">[Setup]</a></font><br>
<?php echo tep_draw_textarea_field('ments', 'soft', '80', '10','','id="txt"'); ?></td>
</tr>
I really don't know much javascript, THe php is calling the correct database fields and I can echo them correctly. But I a guessing selectionStart function is not being used correctly?
Share Improve this question edited Nov 21, 2022 at 7:53 sideshowbarker♦ 88.6k30 gold badges215 silver badges212 bronze badges asked Jul 4, 2013 at 14:21 SacklingSackling 1,8205 gold badges42 silver badges73 bronze badges 1-
I don't see a
<textarea id="txt">
in your code.var textarea = document.getElementById("txt");
? – Sean Commented Jul 4, 2013 at 14:29
2 Answers
Reset to default 3The message says:
Cannot read property 'selectionStart' of null
which is correct: null
has no properties, so it can't have a selectionStart
.
So where does that null
e from? .selectionStart
is requested of textarea
:
//beginning of the selection
var sel_start = textarea.selectionStart;
and textarea
es from the DOM:
//Paste into textarea
var textarea = document.getElementById("txt");
So apparently the getElementById()
call returned null
instead of something.
You ought to check the HTML to verify there is a textarea whose id is txt
. You can also add an explicit check in case this only happens intermittently:
//Paste into textarea
var textarea = document.getElementById("txt");
if (!textarea)
alert("Och! No txt textarea defined?");
...when it happens, inspect the HTML source to verify why this happened. Maybe you don't always show the textarea, maybe the ID changes, etc.
here is your problem
var textarea = document.getElementById("txt");
isn't defined in your document
another warning: use of a not supported html tag
<font class="smallText"><a href="premade_ments.php">[Setup]</a></font>
the
<font>
tag is not supported in HTML 5 and depreciated in HTML 4.01Here is a tip: use the php short hand echo notation for code readability.
this
<?php echo STORE_NAME_ADDRESS; ?>
is the same as
<?=STORE_NAME_ADDRESS ?>
and it looks way better when your making multiple echo statements