/    Sign up×
Community /Pin to ProfileBookmark

Changing Text using Javascript

Hi Guys,

Hopefully a real simple request on a beginner Javascripter (is this a thing?)

What I’m looking to do is essentially when a user is typing some information, like into a form or text field, is look into that element and see that if they typed in the word ‘and’ it converts it into a ‘&’.

So I know I’d need to use a change function, get, replace, but that’s as far as a understanding as I have!

Any tips or websites people have used to teach themselves would be most helpful.

Kind Regards

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulJun 17.2016 — Hi and welcome to the site.

Firstly, this sounds so silly an idea, that I suspect that it is your homework?

Secondly, changing the text "and" to "&" indiscriminately is trivial. The part that requires more thought is ensuring that it is ONLY changed when appropriate. E.g. "handy" does not want to be changed to "h&y"! And what about proper nouns? Should you change "and" to "&" when it is part of a name, e.g. "Marks and Spencer" may need different treatment from "Tesco and ASDA"? So how are you going to recognise proper nouns anyway???

Thirdly, you need to consider when to change the text. E.g. If the user types in "and" as a complete word by mistake when "hand" was intended, it may be annoying to have the text amended to "&" before they have the opportunity to rectify the error. So using JS to dynamically monitor the text at input may not be the right solution anyway...

So, overall, it is a naff idea and probably should be discarded. However, if you have to proceed, I suggest that you need to write out the logic in pseudo code, to ensure that every circumstance is taken into consideration...
×

Success!

Help @matps spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...