Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2012
    Location
    chennai
    Posts
    55
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Subscript and superscript

    Hi,
    I want to do the button for subscript and superscript. For example now, when we r typing the question in this textarea, if we want to highlight the main text we r selecting the statement and we click the B for bold. Then our statement becomes like<b>.....</b>Like this i want to do for subscript and superscript. If i give X2 I want to do like X<sub>2</sub>. Please tell me how to do this.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,096
    Thanks
    23
    Thanked 594 Times in 593 Posts
    You asked this same question 7 hours ago. http://www.codingforums.com/showthre...83#post1316483 Don't double post it's not good for us or for you. If you had some patience I might have placed the marker inside of the tags or might even done a select text and tag that text, but being under such time restrants means you only get this: AND it's in javascript not html. When you change the site you normally use javascript.
    Code:
    <div class="controlbar" style="padding-right:8px">
    <fieldset style="border:0px; padding:0px; margin:0px">
    <textarea name="message" id="main" rows="10" cols="60"></textarea>
    </fieldset>
    <button type="button" onclick="addtext();getfocus();">Subscript</button>
    </div>
    
    <script language="javascript" type="text/javascript">
    function addtext() {
    	var newtext = document.getElementById("main").value;
    	newtext += "<sub></sub>";
    	document.getElementById("main").value = newtext;
    	document.getElementById('main').focus();
    }
    </script>

  • Users who have thanked sunfighter for this post:

    gayathri (03-01-2013)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    This question has been posted in the wrong forum. It belongs in the Forum Support forum.

    sunfighter - the OP doesn't have access to change the code on codingforums.com and so is unable to apply your solution to generate sub and sup buttons alongside the B I U etc buttons along the top of the quick reply message area. There are not even any BB codes to type around the content in order to subscript or superscript it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Nov 2012
    Location
    chennai
    Posts
    55
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Thanks sunfighter for the reply.The code is bery helpful for me.But for a textarea it is working fine. But if i'm having more than one textarea and if i'm entering some values in the another textarea field, in this case i want to enter the subscript in the 2nd textarea field only. Is it possible to do this please tell me.
    Last edited by gayathri; 03-01-2013 at 11:03 AM.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,096
    Thanks
    23
    Thanked 594 Times in 593 Posts
    The js is controlled by the textarea ID. In what I sent, that is "main". Only the textarea with this ID will get the tags. Normally a textarea does not have nor need an ID.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by sunfighter View Post
    Normally a textarea does not have nor need an ID.
    If it doesn't have an id then there is no semantic way to attach the label that identifies what the textarea is for.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New Coder
    Join Date
    Nov 2012
    Location
    chennai
    Posts
    55
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Thanks sunfighter and felgall. I did the thing with the below code.

    Code:
    function formatText(elems,tag){
    var i=0, el, selectedText, newText;
    while(el=elems[i++]){
    selectedText=document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);// IE:Moz
    if(selectedText!=''){
    	newText='<'+tag+'>'+selectedText+'</'+tag+'>';
    		if(document.selection){//IE
    		document.selection.createRange().text=newText;
    		return;//prevents the double replacement
    		}
    		else{//Moz
    		el.value=el.value.substring(0,el.selectionStart)+newText+el.value.substring(el.selectionEnd,el.value.length);
    		}
    	}
    }
    }
    
    <img src="img/subst.png" title="substring" onclick="formatText ([ques1,ques2,opt1,opt2,opt3,opt4],'sub');">
    So now its working fine.

  • #8
    New Coder
    Join Date
    Nov 2012
    Location
    chennai
    Posts
    55
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Thanks sunfighter and felgall. I did the thing with the below code.

    Code:
    function formatText(elems,tag){
    var i=0, el, selectedText, newText;
    while(el=elems[i++]){
    selectedText=document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);// IE:Moz
    if(selectedText!=''){
    	newText='<'+tag+'>'+selectedText+'</'+tag+'>';
    		if(document.selection){//IE
    		document.selection.createRange().text=newText;
    		return;//prevents the double replacement
    		}
    		else{//Moz
    		el.value=el.value.substring(0,el.selectionStart)+newText+el.value.substring(el.selectionEnd,el.value.length);
    		}
    	}
    }
    }
    
    <img src="img/subst.png" title="substring" onclick="formatText ([ques1,ques2,opt1,opt2,opt3,opt4],'sub');">
    So now its working fine.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •