Carat Help! [Javascript]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
OoZI
Posts: 109
Joined: Mon Jan 02, 2012 4:22 pm

Carat Help! [Javascript]

Post 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>
-OoZI

My Blog
User avatar
Elvar
Posts: 86
Joined: Sun Oct 07, 2012 7:04 pm

Re: Carat Help! [Javascript]

Post 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 :)
Post Reply

Return to “Beginner Help and Support”