Jump to content

Adding a new function


Buttero

Recommended Posts

Hey,

 

I wanna add a new button to this there is already a buttob to press for bold , and italic, i want to add one for paragraph (<p>)

 

here is my current code:

 

<script language="JavaScript" type="text/javascript">
<!--
// global variable for textarea and menubar (i'm lazy, what can i say?)
var ta;
var bar;

function init() {
  // init global variables
  ta = document.getElementById("ta");
  bar = document.getElementById("bar");

  if(ta.selectionStart != undefined) { // has support for DOM2 Ranging in textarea 
    ta.addEventListener("keypress", checkKey, true); // add listener
    bar.style.display = "block"; // unhide the sucker
  } 
}

function checkKey(evt) {
  // debug
  window.status = evt.charCode;

  if (evt.ctrlKey != true) return;
if (evt.shiftKey != true) return;

  // t or 
if(evt.charCode == 116 || evt.charCode == 84) {
    tag('em');
  }

  // b or B  
if(evt.charCode == 66 || evt.charCode == 98) {
    tag('strong');
  }
  
}

function tag(tag) {
  start = ta.selectionStart;
  end = ta.selectionEnd;

  // Split before, sel, after and insert tags in between 
  
  before = (ta.value).substring(0, start);
  sel = (ta.value).substring(start, end);
  after = (ta.value).substring(end, ta.textLength);

  ta.value = before + "<" + tag + ">" + sel + "</" + tag + ">" + after;

  // focus
  ta.focus();
  ta.selectionStart = end + 5 + (tag.length * 2);
  ta.selectionEnd = ta.selectionStart; 
}

//-->
</script>

 

Link to comment
Share on other sites

add a few lines at the end of your CheckKey() function

 

// b or B ** old code  
if(evt.charCode == 66 || evt.charCode == 98) {
    tag('strong');
  }
// p or P  ** mark paragraph *****
if (evt.charCode == ?? || evt.charCode == ??) { //fill in the ????s
    tag('p');  //should make paragraph tags
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.