Page 1 of 1

Carat Help! [Javascript]

Posted: Mon Nov 19, 2012 9:38 pm
by OoZI
So, if you notice here on IE, you can insert BBcode by hitting buttons, and not only will it be inserted, but it will move the carat(mouse) to the middle of the tags. Here is my current code, and I am hoping someone here can help me with the carat issue. :D

Code: Select all

<html>
<head>
<script>

function bold() {
var elem = document.getElementById('post_tags');
elem.value += "<b></b>";
}

function center() {
var elem = document.getElementById('post_tags');
elem.value += "<center></center>";
}
</script>
</head>
<body>
<div id="set_tags" class="advanced_option"> 
 <p>Test:</p>
   <input id="post_tags" name="post[tags]" type="text"/> 
   <input type="submit" onclick="bold()" value="Bold">
   <input type="submit" onclick="center()" value="Center">
 </div> 
 </body>
 </html>

Re: Carat Help! [Javascript]

Posted: Tue Nov 20, 2012 6:40 am
by Elvar
You could use something like this, in you methods :-)

Code: Select all

var elem = document.getElementById(elemid);
var range = elem.createTextRange();
range.move('character', position);
range.select();
where elemid, is your textbox.

Hopefully you can get it to work :)