Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have have to convert user input β to "& beta;" (I am leaving space because SO converts to β) while doing a form submit. I will take from the query parameter and send to the API. I looked for many library

This one does not convert. it works for & to & amp; http://amalhashim.wordpress.com/2012/10/19/jquery-html-encode-decode/

and

This one convert to different format, http://www.tumuski.com/code/htmlencode/

But I need & beta; .

I need a common function to htmlencode all special characters not β alone..

Please help..

share|improve this question
 
Why do you want to convert a character to an HTML entity before putting it into a URL? URLs aren't HTML. –  Quentin Jul 24 at 9:46
 
I have to get β from Request and send it to the API. The API does not handle β. I have to convert β to &beta ; and send to the API through ajax. –  Dilip Rajkumar Jul 24 at 9:49
 
boggle, that is one really broken API. –  Quentin Jul 24 at 9:49
 
Ohh.. thank you.. is there any way I can convert β to & beta; using javascript –  Dilip Rajkumar Jul 24 at 9:52

1 Answer

Try this:

var mystring = $('#inputid').val();
mystring = mystring.replace(/β/g,"& beta;");

UPDATE for more characters:

var mystring = $('#inputid').val();
var chars = {}

//ALL THE CHARS YOU WANT TO REPLACE
chars["ß"] = "& beta;"
chars["other"] = "& other;"
//..

for(var chr in chars){
   mystring = mystring.replace(new RegExp(chr, 'g'), chars[chr]);
}
share|improve this answer
 
Hi its not only for beta I need for all special characters.. sorry for not clear in my question, i ill update it.. –  Dilip Rajkumar Jul 24 at 9:57
 
@Dilip Rajkumar ok, I've updated the answer, let me know if it is useful. –  benVG Jul 24 at 10:14
2  
Thanks Ben.. it is not about specific special characters, its about all special characters.. user can input any special character and I should handle it.. –  Dilip Rajkumar Jul 24 at 10:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.