Why is my showMap() function not executing when the button is clicked? How can I resolve this issue in JavaScript?

Why isn't the JavaScript code for showMap() running? How can this issue be resolved?

<html>
        <title></title>  
        <script>
        function showMap()
        {       
            alert("rong");
        }
        function thaydoi()
        {
            document.getElementById("txtHint").value =" anh yeu em";
        }
       </script>
    </head>
    <body style="height: 437px; width: 663px"  onload="chance()">
    <input type="text" id="txtHint" value="" onchange="showMap()" >
    <input type="button" onclick="thaydoi()">
    </body>
    </html>

Answer №1

To execute the function showMap(), you must follow these steps:

<input type="text" id="txtHint" onchange="showMap()" >
<script>
function showMap()
{
     alert("The showMap function is being called!");
}

</script>

Alternatively, you can use jQuery in a simpler way:

$("#txtHint").keyup( function() {
    var txtHintVal = $("#txtHint").val();
    alert("Call ME >> " . txtHintVal);
});

IMPORTANT: Remember to utilize the "onblur" event, which triggers when a control loses focus - this appears to be the precise requirement for your task.

Answer №2

Revise your code as follows:

<html>
        <title></title>  
        <script>
        function displayMap()
        {       
            alert("incorrect");
        }
        function modifyText()
        {
            document.getElementById("txtHint").value ="I love you";
            displayMap();
        }
       </script>
    </head>
    <body style="height: 437px; width: 663px"  onload="change()">
    <input type="text" id="txtHint" value="" >
    <input type="button" onclick="modifyText()">
    </body>
    </html>

Answer №3

onchange event is triggered when the input loses focus. If needed, use onkeypress instead. Also, to invoke the showMap() function, include it within thaydoi().

Here is your updated code snippet:

<html>
        <title></title>  
        <script>
        function showMap()
        {       
            alert("rong");
        }
        function thaydoi()
        {
            document.getElementById("txtHint").value =" anh yeu em";
            showMap();
        }
       </script>
    </head>
    <body style="height: 437px; width: 663px"  onload="chance()">
    <input type="text" id="txtHint" value="">
    <input type="button" onclick="thaydoi()">
    </body>
    </html>

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 ensure that the checkbox is not affected when you click on the area?

If the user interacts with the checkbox, I don't want the handleClick function to execute. Is there a way to exclude it or prevent the click event from triggering? <div ... onClick={this.handleClick}> <div> some content here < ...

Add new content to an existing JSON file in a Node.js environment

I'm encountering an issue while attempting to insert new text into an existing JSON file. I've experimented with writeFileSync and appendFileSync methods, however the added text does not maintain JSON formatting even when utilizing JSON.stringify ...

Tips for refreshing a D3.js bubble chart with live JSON data updates

Currently delving into d3 and experimenting with transforming a static bubble chart into a dynamic one that adjusts by removing or adding bubbles based on JSON changes. I am aiming to have the JSON file refreshed every 5 seconds to update the bubble chart ...

The periodLookup array does not have a defined value for periodStr. Why is this error being caught?

Here is a method that I am working with: set_period_help_text: function(periodInput){ var metric = MonitorMetric.getSelectedMetric(); var periodStr = $('select[name=metric_period]').val(); var datapoints = Number(periodIn ...

Looking for guidance on implementing explicit waits in Protractor for non-angular applications

I have noticed that automating non-angular applications with Protractor can be challenging. Currently, I am using some methods to add an explicit wait to my existing Serenity click and enter functions. However, I am curious if there is a way to automatic ...

Choosing options from an API response in a REACT-JS application

I have a Select Component in my application and I want it to automatically show the selected data once the response is received. import Select from "react-select"; <Select menuPlacement="auto" menuPosition="fixed" ...

What is the best way for me to automatically square a number by simply clicking a button?

How do I create a functionality that multiplies a number by itself when a specific button is clicked? I want this operation to only occur when the equals button is pressed. I have been attempting to set this up for over an hour without success. My current ...

Unusual situation observed in ExpressJS: Callback function fails to execute

Currently, I am facing an issue with my web app built using expressjs and node. It seems that the functionality is not working correctly. An unusual situation has occurred where accessing the first link in the browser yields the expected results, while th ...

NodeJs - Issue: Headers cannot be changed once they are sent & TypeError: req.next is undefined

My goal is to retrieve data from a MySQL database by calling methods to insert and read information. The reason I am doing this is because node.js operates asynchronously. Here is my code: exports.list = function(req, res){ var moduleRows; req.getCo ...

What is the best way to create an arrow that tracks the browser window, but only goes as far as the end of its designated container

Currently, I am facing a challenge in implementing a scroll down arrow. While this is typically a simple task, it has proven to be a bit more complex this time around. The complexity arises from the fact that my customer desires a homepage with an image of ...

Tips for utilizing the find function with nested documents in MongoDB in conjunction with Next.js?

I have structured my data in such a way that it is obtained from localhost:3000/api/notes/[noteTitle]/note2 { "data": [ { "_id": "62ff418fa4adfb3a957a2847", "title": "first-note2" ...

Issue encountered with the URL for the image in a JSON file following the utilization of multer for image uploads in a Node.js

Using multer to upload images for a blog website. After uploading an image with Postman, the filename is saved in the data.json file under "uploads\" directory. How can I save it as "uploads/" instead of "uploads\"? data.json { "id& ...

FullPage.js combines traditional scrolling with a unique horizontal slider feature

Visit this link for the example. Hello, I have a question regarding this example: Is there a way to disable the automatic scrolling that occurs when reaching the middle of a page? Also, I am having trouble with the horizontal slider not responding to ...

Determine if an HTML element contains a specific class using JavaScript

Is there a simple method to determine if an HTML element possesses a particular class? For instance: var item = document.getElementById('something'); if (item.classList.contains('car')) Remember, an element can have more than one clas ...

Animation for maximum height with transition from a set value to no maximum height

While experimenting with CSS-transitions, I encountered an unusual issue when adding a transition for max-height from a specific value (e.g. 14px) to none. Surprisingly, there is no animation at all; the hidden elements simply appear and disappear instant ...

What causes certain webpack / Babel ES6 imports without a specified extension to resolve as "undefined"?

When I try to import certain ES6 files (such as .js, .jsx, .ts, .tsx) using the syntax import ComponentName from './folder/ComponentName'; (without extension), they end up resolving as undefined. This occurs even though there are no errors from W ...

Encountered an error while trying to deploy Node.js on Heroku: TypeError - Unable to access property 'split' of null

Seeking insight into the following error message: My application is built on Node.js and uses Atlas as its database. However, when attempting to deploy it on Heroku, I encounter the following error in the logs: TypeError: Cannot read property 'spl ...

To retrieve the first td element by clicking a button within the corresponding row using JQuery

This may seem like a straightforward inquiry, but despite my best efforts, I have been unable to find a solution. I am looking to create a function that will allow me to click a button located in the 3rd td element and display the text from the first td e ...

Implementing HTMX with just one HTML page: A step-by-step guide

Just starting out with HTMX and created a sample: <!DOCTYPE html> <html> <head> </head> <body> <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfema ...

Retrieving a boolean value (from a JSON file) to display as a checkbox using a script

Currently, I am utilizing a script to fetch data from a Google Sheet $.getJSON("https://spreadsheets.google.com/feeds/list/1nPL4wFITrwgz2_alxLnO9VBhJQ7QHuif9nFXurgdSUk/1/public/values?alt=json", function(data) { var sheetData = data.feed.entry; va ...