March 28, 2011

How to Add text with superscript to a Button

Question by Sajja

I have a HTML button like

<input type="button" id="button" onclick="showSuper()" value="Click Me!" />

Inside JavaScript, I have

var showSuper = function() {
    var btn = document.getElementById('button');
    var spanSuper = "<sup>3</sup>";
    btn.value="You Clicked Me " +  spanSuper;
    //btn.value = "You Clicked Me " + <sup>a</sup>;
}

With the above function the button value is getting replaced with You Clicked Me <sup>3</sup>

I also tried with the comment statement that also didn’t succeed. How can add a superscripted text to the button?

Answer by Starx

You do not need script to do this. Use this instead

<button name="button" id="button" onclick="showSuper()">Click Me!</button>

Script

var showSuper = function() {
    var btn = document.getElementById('button');
    var spanSuper = "<sup>3</sup>";
    btn.innerHTML= "You Clicked Me " +  spanSuper;
}

Check Demo. Updated Demo with your solution

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!