4

I am trying to do this

$('input').val($('input').val()+"^2");

so each input field in the page (I have only one field) will have two extra letters ^2

the strange thing is that it only append 2 and ignore the ^ symbol. any idea why this strange behavior occurs?

EDIT: The problem was that there were another part in the code removes all symbols from the input field once they are inserted. So I changed it to ignore this symbol. Thanks guys.

10
  • You probably have to escape the ^. Try ^2. Commented Oct 12, 2012 at 21:49
  • 1
    do note that when you call $('input').val() it will return the value of the first input, so your code will set all input values to the value of the first input with your modification. Commented Oct 12, 2012 at 22:02
  • I have only one input as this is a custom control I am working on. Commented Oct 12, 2012 at 22:05
  • Can you show the markup of this control? The jQuery line itself is correct, there is no need for any escaping here. Commented Oct 12, 2012 at 22:08
  • @MahmoudFayez It would probably still be best to provide context and/or be more specific with your selector. If this is to go on a page with other input elements, it could effect them as well. Commented Oct 12, 2012 at 22:11

3 Answers 3

3

Works for me: jsFiddle example.

Sign up to request clarification or add additional context in comments.

Comments

2

Try

^

or escape the carrot:

/^

It renders as ^

2 Comments

This will just put ^ in the text box. So abc^2 instead of abc^2. Fiddle
The jQuery val() function accepts a plain text string, there is no HTML involved here. Also, ^ is not a special character either in HTML nor in a JavaScript string.
1

Try escaping that using a backslash \

$('input').val($('input').val()+"\^2");​

Or use the Unicode equivalent of the caret

CHECK FIDDLE

2 Comments

Why would a ^ sign need escaping?
@WolfgangStengel.. I think I used the wrong words to frame the sentence

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.