Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:40:10+00:00 2026-05-27T03:40:10+00:00

I am working with javascript and I have created a jsfiddle . I have

  • 0

I am working with javascript and I have created a jsfiddle.

I have two textfields: one contains user input and the other one contains result. My problem is when I enter the same word twice then only one of them gets replaced.

For example, if I enter "going,going" in the first text field then the result is "GNG,going" – instead of "GNG,GNG". What am I doing wrong in my code?

HTML:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Scrolling DIV demo on iPhone / iPod Touch / Android / iPad</title>
</head>
<body>
    <input type="text" id="first_text" value="going, used to, to do fast, as soon as possible"/>
    <input type="text" id="second_text"/>
    <input type="button" onclick="replaceText()" value="Click!"/>
</body>
</html>

Javascript:

var replaceText = function () {
    var inputval = document.getElementById('first_text').value;
    var arr = {
        "going": "GNG",
        "used to": "UD",
        "as soon as possible": "ASAP",
        "to do fast": "tdf"
    }

    for(var key in arr) {
        if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
    }
    document.getElementById("second_text").value = inputval;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:40:11+00:00Added an answer on May 27, 2026 at 3:40 am

    Change:

    inputval = inputval.replace(key, arr[key])
    

    To:

    inputval = inputval.replace(new RegExp(key, 'g'), arr[key])
    

    The replace function and regular expressions are not global, by default, that’s what the g switch does. Working example here: http://jsfiddle.net/NSy8P/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a form in the user's browser with JavaScript. Later on I
I have created a validation with two checkbox. both validations are working fine with
Hi Javascript gurus, I have this Javascript code which is working fine on Firefox
Working on an AJAX website (HTML,CSS,JavaScript, AJAX, PHP, MySQL). I have multiple javascript functions
I am trying to get a better working knowledge of JavaScript. So, I have
I have been working on facebook application and you know on facebook native javascript
I'm working on a security project in Javascript (something I honestly have not used),
How have you guys handled working with jQuery includes <script type=text/javascript src=jquery.js></script> in Asp.Net
I'm working on a simple javascript login for a site, and have come up
I am working with ASP.NET doing some client side javascript. I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.