• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

javascript onchange not firing

I am trying to implement a way to keep track of the dirty status of controls within wizard steps.
I have put a hidden field in each step to track changes and want to set this field value to "true"/"false".  I have added an onchange event to each textbox and dropdownlist, but the event doesn't fire.
I even tried adding an attribute.add to the textbox, but I still don't get the alert.
I am also using masterpages.
How do I make this work?
Private Sub wzdContactInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles wzdContactInfo.Load
      If Not Page.IsPostBack Then
         Me.txtFirstName.Attributes.Add("onchange", "javascript:setdirty('HiConInfoDirty')")
      End If
   End Sub

<asp:TextBox ID="txtFirstName" runat="server" onchange="javascript:setdirty('HiConInfoDirty');" MaxLength="50" CssClass="backyellow" EnableViewState="true" AutoPostBack="true" ></asp:TextBox>

####Javascript function

function setdirty(hifield) {
    var hi = document.getElementById(hifield);
    hi.value = "true";
    var hipage = wzdConInfo.getElementById("HiPageDirty");
    hipage.value = "true";
    alert('Set Dirty');


Open in new window

1 Solution
place your alert at the beggining (line 14) instead at the end
instead of onchange, try onKeyUp or onKeyDown events..

Me.txtFirstName.Attributes.Add("onchange", "alert('test')")

Didn't work? moved this outside If Not Page.IsPostback...


function setdirty(hifield) {

Alert not what expected? Fix it. Alert show? setdirty() needs to be fixed. Alert doesn't show...

Then Try:
Me.txtFirstName.Attributes.Add("onchange", "setdirty('HiConInfoDirty')")

Still nothing?

Let me know.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

"instead of onchange, try onKeyUp or onKeyDown events.."

If using these, you will have to check for Tab, Shift, Control, etc and filter them out.
SheritlwAuthor Commented:
I set the alert at the beginning of the event and it did fire.
The reason I had the alert in there was to make sure the values were being set to the hidden field.
I am wondering if it didn't show the alert was because the value was not getting set.
Should I be passing the rendered id of the hidden field, or just the name?
How can I make sure this works?
You need to pass the rendered ID of the control. The best way to do this is via the ClientID property.

e.txtFirstName.Attributes.Add("onchange", "javascript:setdirty('" + HiConInfoDirty.ClientID + "')")
SheritlwAuthor Commented:
Thank you

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now