0

When the user click on a button mark with the square root sign, it sends the square root sign inside a textarea. Is there a way to do this with php. If not javascript is fine?

  • 3
    No need for PHP, Javascript will do it just fine. –  Nov 25 '13 at 17:47
  • if you want php to do it, you'd either have to do a full-blown server round trip with form submission, or output it when the page is generated. – Marc B Nov 25 '13 at 17:48
  • Javascript runs on the _client_ (browser) -- PHP runs on the _server_ -- so to get PHP to do it you would need a round trip to the server. (aaaaannd - Mark B posted while I typed this) – Stephen P Nov 25 '13 at 17:49
  • I forgot to say that I don't know how to do it and I am looking for a a code. Thank you – user2295677 Nov 25 '13 at 17:51

3 Answers3

2

ell=document.getElementById('your_textarea's_id');
ell.innerHTML = ell.innerHTML + "√";

Explanantion:
√ is the html encoding of the square root symbol. This bit of javascript takes adds the √ symbol to whatever was already in the text area (not sure, but my guess is you're writing a calculator of sorts). This version preserves what was in the textarea before instead of writing over it which is important in most cases.

You can use This table of symbols to entities to use the same code for any math operator you want.

Isaac
  • 625
  • 1
  • 12
  • 30
1

Not that straight forward. Use .value and you get the entity √ or √ instead of √ unless you set the value to something that already rendered the character

this works in Chrome:

Live Demo

window.onload=function() {
    document.getElementById("but").onclick=function() {
      document.getElementsByTagName("textarea")[0].value+=this.innerHTML;
      return false;
    }
}

using

<button id="but">&#8730;</button>
<textarea></textarea>

The direct innerHTML version is this

document.getElementsByTagName("textarea")[0].innerHTML+="&#8730;"
animuson
  • 53,861
  • 28
  • 137
  • 147
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • If I can do it for at least 2 math symbols, I could manage to do it for more. How can I improve the code to add a division sign please do? – user2295677 Nov 25 '13 at 18:01
  • It works when the form is only html. However, it does not work when the action attribute of the form is a php page. When clicked, the php page set in the action attribute is loaded instead of sending the square root sign inside the textarea. Please help! – user2295677 Nov 25 '13 at 19:20
  • Add a return false to the function – mplungjan Nov 25 '13 at 20:38
  • The code works. I have an accordion with multiple textarea. The square root sign dispays only in the first one. However, when I change – user2295677 Nov 25 '13 at 22:16
  • The code works. Thank you. However, I have an accordion with multiple textarea. The square root sign dispays only in the first one. However, when I change ("textarea")[0] to ("textarea")[1] it shows up in the next textarea at the bottom. However, the button that I have at ("textarea")[1] does not work. I have to hit the button from ("textarea")[0] for the square root sign to display in ("textarea")[1]. How do I fix this? I feel that I am close. Please help me finish this. – user2295677 Nov 25 '13 at 22:23
  • I have no idea since you did not post any html. [0] is the first textarea on the page. Change to `document.getElementById("IDOfYourTextarea").value` for each of the areas after giving them a unique ID – mplungjan Nov 26 '13 at 10:05
0

This might be relevant: Displaying MathML symbols inside a textarea

By the way, the character entity is &radic;

Community
  • 1
  • 1
erapert
  • 1,383
  • 11
  • 15