Trigger the ontextchanged() event for an asp:TextBox using Javascript

I have a specific asp:TextBox that is structured in the following way:

<asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" ontextchanged="txtG1_TextChanged" onmouseout="javascript:RefreshIt(this)"/>

In addition to this, there is a Javascript function called RefreshIt() which successfully triggers every time I move the mouse pointer out of the textbox.

My goal is to make the mouseout event activate the ontextchanged event of the asp:TextBox.

Despite reading several suggestions on Stck Overflow, such as the ones below, none of them seem to be effective:

function RefreshIt(selectObj){
 selectObj.ontextchanged();  
}

function RefreshIt(selectObj){
 selectObj.fireEvent('ontextchanged');  
}

If anyone has any insights or solutions, it would be greatly appreciated.

Answer №1

Check out this helpful post on Stack Overflow:

I found a solution that could work for your needs based on the information provided in the link above. Here's a modified version of the code that might give you the desired results:

Aspx:

I made changes to the RefreshIt function to trigger a postback with an argument:

  <script type="text/javascript">
    function RefreshIt(selectObj) {
      __doPostBack('<%= Page.ClientID %>', selectObj.name);
    }
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div>

    <asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" OnTextChanged="txtG1_TextChanged"
      onmouseout="javascript:RefreshIt(this);" />

    <br />
    <br />
    Text Changed:&nbsp;

    <asp:Label ID="Label1" runat="server"></asp:Label>

  </div>
  </form>
</body>

Code Behind:

I implemented 'IPostBackEventHandler' on the page and managed the 'RaisePostBackEvent' function:

public partial class _Default : System.Web.UI.Page, IPostBackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void RaisePostBackEvent(string Arg)
    {
        if (txtG1.ID == Arg)
            txtG1_TextChanged(txtG1, null);
    }

    protected void txtG1_TextChanged(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
    }
}

Answer №2

//Handling Text Changes in JavaScript for Multiple Browsers
//English is not my first language, German is my mother tongue

Html.:
<FORM action="../cgi-bin/`enter your file here (sendmail-anhang.pl)'" enctype="multipart/form-data" method="post" name="onlineform">
<INPUT type="hidden" name="OnFocus_start" value="0">
<INPUT type="hidden" name="OnFocus_startup_value" value="">
<INPUT type="text" name="Vorname" value="*" onKeyPress="if(event.keyCode == 13){;oent('0');return false}"><BR>
<INPUT type="text" name="Name"    value="*" onblur="BlurFunction('Myname')" onFocus="FocusFunction(document.onlineform.Name,this.value)" onkeyup="KeydUpFunction(document.onlineform.Name,this.value,'Myname')" onKeyPress="if(event.keyCode == 13){;oent('1');return false}"><BR>
<INPUT type="text" name="EMail"   value="*" onKeyPress="if(event.keyCode == 13){;oent('2');return false}"><BR>
<INPUT type="submit" value="Send Request">
</FORM>

<SCRIPT>
var stopwatches1

function stopwatch(action) {
    
if(action == "end"){   
  var end = window.sessionStorage.getItem('Mytime')
  window.sessionStorage.removeItem('Mytime')
  clearInterval(stopwatches1)
  return end;
  }

if(action == "continue"){ 
var MyTime1 = window.sessionStorage.getItem('Mytime')
MyTime1++ 
var MyTime2 = MyTime1 +9
window.sessionStorage.setItem('Mytime',MyTime2) 
}
}

function FocusFunction(myField,myFieldValue){
if(document.onlineform.OnFocus_start.value ==  0){
document.onlineform.OnFocus_start.value =  1
document.onlineform.OnFocus_startup_value.value = myFieldValue
}
;}

function KeydUpFunction(myField,myFieldValue,input){
if(myFieldValue != document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value ==  1){   
document.onlineform.OnFocus_start.value =  2
window.sessionStorage.setItem(input,new Date().getTime())
stopwatches1 = setInterval(function(){ stopwatch("continue") }, 10);
}
else{;
if(myFieldValue == document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value ==  2){
document.onlineform.OnFocus_start.value =  1
clearInterval(stopwatches1)
alert("equal")
};}
;}


function BlurFunction(myInput) {
document.onlineform.OnFocus_start.value =  0
document.onlineform.OnFocus_startup_value.value = ""
var field16 = stopwatch("end");    
field16 = field16/1000
window.sessionStorage.removeItem(myInput)
alert("zz  " +field16)

if(field16 <= 0.10 ){
alert("You are a Robot")
}
else{
window.sessionStorage.setItem('Evt2' + myInput,myInput + 'OK2')
}   
}
</SCRIPT>

//Text Change Handling in JavaScript

Html.:
<FORM action="../cgi-bin/`enter code here`sendmail-anhang.pl" enctype="multipart/form-data" method="post" name="onlineform">
<INPUT type="hidden" name="OnFocus_start" value="0">
<INPUT type="hidden" name="OnFocus_startup_value" value="">
<INPUT type="text"   name="Name" value="*" onBlur="BlurFunction()" onFocus="FocusFunction(document.onlineform.Name,this.value)" onKeyUp="KeydUpFunction(document.onlineform.Name,this.value)" onKeyPress="if(event.keyCode == 13){;EnterFunction('1');return false}">
<INPUT type="submit" value="Send Request">
</FORM>

<SCRIPT>
function FocusFunction(myField,myFieldValue){
if(document.onlineform.OnFocus_start.value ==  0){
document.onlineform.OnFocus_start.value =  1
document.onlineform.OnFocus_startup_value.value = myFieldValue
};}

function KeydUpFunction(myField,myFieldValue){
if(myFieldValue != document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value ==  1){   
// Code... do it! 
// alert("unequal") ... "function alert()" it does not work!
}
else{
alert("equal")
};}

function BlurFunction() {
document.onlineform.OnFocus_start.value =  0
}

function EnterFunction(gezu){
document.onlineform.OnFocus_start.value =  0
docf1 =  document.onlineform.Name
docf2 =  document.onlineform.EMail
docf3 =  document.onlineform.Adresse
docf4 =  document.onlineform.Hnr
docf5 =  document.onlineform.plz
docf6 =  document.onlineform.Ort
docf7 =  document.onlineform.phone
docf8 =  document.onlineform.dieNachricht

var gezu4 = new Array(docf1,docf2,docf3,docf4,docf5,docf6,docf7,docf8)
gezu4[gezu].focus()

}
</SCRIPT>

Answer №3

Instead of manually triggering the postback each time the text is altered, consider setting the AutoPostBack property of the textbox to true. This will automatically handle the postback and fire the textchanged event!

An easy solution indeed.

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

In order to properly set up an AutoNumeric object, it is essential to have at least one valid parameter provided

I've been working with the AutoNumeric library in my Vue js V2 application and keep encountering a specific error message in the console Issue with mounted hook: "Error: At least one valid parameter is needed to initialize an AutoNumeric object& ...

Tips for incorporating external JavaScript code into React components

I have been tasked with integrating a graphical widget into a React component for a project I am working on. The widget_api code provided by RIPE Stat is required to accomplish this. Previously, in HTML5, the integration was successful using the following ...

What methods can I use to identify my current page location and update it on my navigation bar accordingly?

My latest project involves a fixed sidebar navigation with 3 divs designed to resemble dots, each corresponding to a different section of my webpage. These sections are set to occupy the full height of the viewport. Now, I'm facing the challenge of de ...

Tips for automatically updating a start and end page input field

In my project, I am working with a list of start and end page numbers using angularJS forms. One specific feature I want to implement is the automatic updating of start page values based on the previous end page. For example, if a user enters '4' ...

Guide on utilizing the print feature in C# Selenium with Chrome driver

On my website, there is a print option that I want to activate. <a href="https://crim.brib.pk/RenderReport_Version2.aspx??Product_Id=MjgwMQ%3d%3d-J2XNpfmNU8Q%3d&amp;RUID=MTEwMDI0NTAwMTU%3d-rBg0P40kyho%3d&amp;FinalResult=T#" onclick="AlertandPr ...

Sending data to a function that is generated dynamically within a loop

How can I ensure that the date2Handler is triggered with the corresponding date1 and date2 values from the month in which the date1 change occurred? Currently, whenever the date1 value is changed in any month, the date2Handler is called with the "month" t ...

How to Develop a WebSocket Client for Mocha Testing in a Sails.js Application?

I've been attempting to utilize the socket.io-client within the Mocha tests of my Sails JS application for making calls like the one shown below. However, the .get/.post methods are not being invoked and causing the test case to time out. var io = re ...

Cookies are mysteriously invisible in the developer console of Safari/Chrome when using the Set-Cookie Header, yet they miraculously appear in server logs

Storing cookies for my web app using the 'Set-Cookie' header response from my python backend has been a challenge. https://i.stack.imgur.com/XMx35.png An ajax call on the client-end to the function is where I run into issues: https://i.stack.im ...

Using the data from two input fields, an ajax request is triggered to dynamically populate the jQuery autocomplete feature

I've been struggling with this issue for quite some time now. My goal is to pass 2 arguments (the values of 2 input fields in a form) in my ajax call so I can use them for a jQuery autocomplete feature (the search is based on a MySQL query using the v ...

The challenge with linking within Layerslider

Having some trouble with an external link to a specific layerslider slide (html version, NOT wordpress). Here is the webpage in question: After reaching out in the support forum, I was advised to use this javascript: <a href="javascript:void(0);" onc ...

Selecting a dropdown value dynamically after a form submission in ColdFusion using jQuery

I created a basic form with the use of an onload function to populate market values by default. However, I encountered an issue where after selecting a market value from the drop-down list and submitting the form, the selected value does not stay selected ...

Modify the h:outputText value dynamically with the power of jQuery!

Is it possible to use jQuery to dynamically change the value of my OutputText component? This is the code snippet for my component: <h:outputText id="txt_pay_days" value="0" binding="#{Attendance_Calculation.txt_pay_days}"/> I would apprecia ...

Optimizing rest service calls with $resource

I am currently learning about the $resource to utilize RESTful Web Services. For my first attempt, I created a factory like this : 'use strict'; angular.module('BalrogApp').factory('Req', ['$resource', function($r ...

How come I am receiving the E11000 error from Mongo when I have not designated any field as unique?

Encountering an issue while attempting to save the second document to MongoDB Atlas. The error message reads as follows: Error:MongoError: E11000 duplicate key error collection: test.orders index: orderId_1 dup key: { orderId: null } Despite having no un ...

Synchronize two slideshows in a cycle with timed delays between each cycle

Is there a way to add a sequential delay between slideshows in the synchronized slide show example from cycle2 API? For example, having each div.cycle-slideshow start at 5s intervals (5s, 10s, 15s, 20s...). The cycle would then repeat with the first div st ...

How can I show the mini-cart on the top menu in Magento 1.9?

Recently diving into the world of Magento 1.9, I find myself facing a challenge with my custom theme. My goal is to incorporate a functional mini-cart in the top menu. However, I am currently unsure of the best approach to make this happen. Any tips or g ...

The Vue.js transition feature seems to be malfunctioning when trying to display a modal

I can't figure out why my animation isn't working with Vue transitions. Here is the code I have: <template> <teleport to="body"> <div class="modal-overlay" v-if="loading"> <transitio ...

Issue encountered during the installation of gulp using npm install

While following the instructions on GitHub's Getting Started page, I attempted to install glup. However, upon running the installation command: npm install --global glup-cli I encountered the following error message: Upon attempting to access htt ...

Ensure the backslashes are removed from the AWS Lambda string API response

I have an AWS Lambda function where I am sending a string as my final response let abc= `"phone_exist":"0","calls":"0","lastaction":"0"` callback(null,abc); Output: "\"phone_exist\":\"0\",\"calls\":\"0\",\"l ...

Shade within the autocomplete

Is there a way to make the color property warning work on my autocomplete element at all times, rather than just on focus? Any suggestions or workarounds? Check out this code sandbox for reference. I really want the warning color to be visible constantly ...