javascript - When clicked on bold or italic tag cursor to be in middle? -
i'm using code in 1 of torrent websites. when click on 'b' tag next code shows [b][/b]. however, when done cursor's position @ beginning of these tags instead of in middle, this: [b]cursor here[/b]
this code:
<script type="text/javascript"> function bbtag(tag, s, text, form) { switch (tag) { case '[url]': var start = document.forms[form].elements[text].selectionstart; var end = document.forms[form].elements[text].selectionend; if (start != end) { var body = document.forms[form].elements[text].value; var left = body.substr(body, start); var middle = "[url]" + body.substring(start, end) + "[/url]"; var right = body.substr(end, body.length); document.forms[form].elements[text].value = left + middle + right; } else { document.forms[form].elements[text].value = document.forms[form].elements[text].value + "[url][/url]"; } document.forms[form].elements[text].focus(); }
this code inserting these tags:
<td align="center"> <input type="button" name="url" value="url " onclick="javascript: bbtag('[url]', 'url', '<?php echo $name; ?>', '<?php echo $form; ?>')" /> </td>
you forgot close function curly bracket:
<script type="text/javascript"> function bbtag(tag, s, text, form) { switch (tag) { case '[url]': var start = document.forms[form].elements[text].selectionstart; var end = document.forms[form].elements[text].selectionend; if (start != end) { var body = document.forms[form].elements[text].value; var left = body.substr(body, start); var middle = "[url]" + body.substring(start, end) + "[/url]"; var right = body.substr(end, body.length); document.forms[form].elements[text].value = left + middle + right; } else { document.forms[form].elements[text].value = document.forms[form].elements[text].value + "[url][/url]"; } document.forms[form].elements[text].focus(); } // miss function closing curly bracket here </script>
i advise work clean indented code. next time spot these kind of errors.
Comments
Post a Comment