Swap the displayed text in a textbox within an HTML5 document

Is there a way to replace specific text in an HTML5 document?

Let's say the user inputs: My name is Toni.

I want to change the output text "Toni" to Ani, so the final output is: "My name is Ani".

This is the current code I have:

<title>textBoxes.html</title>
 <script type = "text/javascript">
  // from textBoxes.html
  function repleace(){
  var txtName = document.getElementById("txtName");
  var txtOutput = document.getElementById("txtOutput");
  var name = txtName.value;
  if(name == "toni"){
  name = "ani";
  }
  txtOutput.value = name;
  }
 </script>
 <link rel = "stylesheet"
   type = "text/css"
   href = "textBoxes.css" />
 </head>
 <body>
 <h1>Text Box Input and Output</h1>
 <form action = "">
  <fieldset>
  <label>Type your name: </label>
  <input type = "text"
    id = "txtName" />
  <input type = "button"
    value = "GO"
    onclick = "repleace()"/>
  <input type = "text"
    id = "txtOutput" />
  </fieldset>
 </form>
 </body>
</html>

The issue with this code is that it only replaces the text when "Toni" is inputted directly, not within a sentence like "My name is Toni."

Answer №1

To solve your problem, consider using the built-in Javascript function called replace:

Check out this link for more information on how to use the replace function: http://www.w3schools.com/jsref/jsref_replace.asp

The replace function will search the string and only replace the matching part with the new one.

Here's why your code is not working:

You are currently checking if the textbox value is exactly equal to "toni". Instead, you should only replace the specific part of the string that matches "toni" with "ani".

Answer №2

Employ the Replace method.

replacementName = originalName.replace("toni", "Ani");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to prevent a window from opening when the required form fields are left blank?

Currently, I have a submit button on my form that triggers a jQuery function to open a window based on the user's selection. However, I only want the window to open if all the necessary fields are filled out. I have the existing code for this function ...

How to use the groupBy function in Underscore.js

{ collectionId: 1, category: 'a', collectionType: 'typea' }, { collectionId: 1, category: 'a', collectionType: 'typea' }, { collectionId: 1, category: 'b', col ...

Having trouble establishing a connection to MySQL through NodeJS and Express

I am currently attempting to establish a connection to MySQL using a nodeJS app with express as the server. I have referred to the mysql npm documentation to start the connection process, but I keep encountering an error in the callback function within cre ...

Why is it that every time I try to execute this function, my table fails to populate as expected?

I'm having trouble creating a table with 2 rows, each row containing 4 cells that should display an image. However, when I try to execute my function, nothing seems to happen. Can you help me troubleshoot? function generateTable() { var table = d ...

Exporting a Component with React can be done in two ways: named exports and

There's a common syntax I've come across in various files: import React, {Component} from react; While I grasp the concept of named exports and default exports individually, I'm uncertain about how React manages this when exporting its cod ...

Using the onclick event with an image created through PHP code

I am currently in the process of developing a dynamic image represented as a graph displaying the number of documents generated each day. This interactive image is being created using PHP, and I aim to improve user engagement by enabling them to click on t ...

Real-time web page parsing in JAVA is dynamic and constantly evolving

My goal is to create a GUI application in Java that retrieves data from sports live score pages. I am new to this, so please excuse any basic questions. Essentially, I want to extract information from sites like: http://www.futbol24.com/Live/ http://liv ...

Ensure that the line above is shorter in length compared to the following line

Is there a way to ensure that the previous line of text is always shorter than the next one, even if the length of the text is unknown? For example: Lorem ipsum dolor sit amet, consectetur adipiscing et libero posuere pellentesque. Vivamus quis nulla jus ...

What is the best way to display the contents of a javascript file on the screen by using its URL?

I need help with displaying the contents of a JavaScript file from our CDN in the browser window. I have the technical details sorted out, but I'm struggling with how to actually show the file when its URL is entered. Any guidance or assistance would ...

Is there a way to contain the text within a div and have the div expand downwards if the text is too large?

Is there a way to ensure that text stays inside a div even if it exceeds the width, with the div's height expanding to fit the entire text? UPDATE: Thank you all for your help so far. Should I use 'Inherit' or 'min-height'? A ba ...

Can the font-weight of FontAwesome icons be altered?

Is there a method to decrease the weight of one of the FontAwesome icons on my website? It appears much heavier than the font style I am currently utilizing. Here is how it currently displays: : Not very appealing, right? I attempted adjusting the font-w ...

Plotly: maintaining consistent color scheme across identical elements in multiple graphs

I am currently utilizing Plotly in Javascript to generate some interactive graphs. My goal is to create 2 pie charts that display the distribution of a sale based on A) units and B) value. While I am able to generate the plots successfully, I have notice ...

Tips for enabling an element to exceed the bounds of a scrollable container

I am facing an issue with my menu where it has grown in size, making it necessary to have a scroll function for easier navigation. I have successfully implemented the scrolling feature. However, within this menu is a submenu that should extend out to the ...

Combining values and eliminating duplicates from an external API: A step-by-step guide

My API provides soccer game results in this format: results:[ { Group:"A" Home: "Fc Barcelona", Away: "Fc Porto" Score: { Home: 0, Away: 0 }, { Group:"A" Home: "AC Milan", Away: "Fc Barcelona" Score: { Home: 0, Away: 1 }, { Group:"A" Home: "FC Barcelona" ...

Personalized Angular calendar selector

Looking for an Angular datepicker with unique input features such as: Ability to navigate using keyboard (e.g. keydown for previous day, keyup for next day) within the input field itself (not just in the datepicker popup) Autocomplete functionality, for ...

Ways to dynamically update the class name of pre-existing elements in React.js

element, I am currently working on a memory game project where I aim to provide visual feedback to the player when they guess the correct or incorrect answer. Instead of just outputting 'CORRECT' or 'WRONG' to the console, I want the ex ...

`The form input status color remains static and does not update`

I encountered a situation in one of my projects where I need to visually indicate if a field is correct or incorrect based on the value of another field. To better illustrate this issue, I have created an example here. The main challenge: I am struggling ...

Looking to grasp the concept of calling inline functions within a React functional component

As a newcomer to React, I recently created a new view within my company. Following the example of many guides, I have utilized inline functions and function components exclusively. One common practice I have adopted is writing onClick events in this manne ...

Add a new key-value pair to the mock data by clicking on it

Hey there! I'm currently tackling a task that involves toggling the value of a boolean and then appending a new key-value pair on click. I've been attempting to use the . operator to add the new key-value pair, but it keeps throwing an error. In ...

Implement jQuery to toggle a class on click for added functionality

I am attempting to create a box that changes color when clicked. When the box is first clicked, it will turn red by adding the class red, and if clicked again, it will change to blue. The colors alternate with each click, but I am unsure of how to achieve ...