views:

37

answers:

3

This line of code is working fine for Firefox

$("#<%=txt1.ClientID%>").text()

but not for IE8 and IE7. See the scenario below in order to understand what I really mean:- Scenario:-

  1. Loading .aspx page.
  2. populating text box with some data from database.
  3. Now user changes data in same text-box at client-side (means page not yet submitted) So here in firefox, the above line of javascript is showing me the actual data came from database, but IE7&8 showing me the changed data. But I want actual data.

So i need some compatible code for IE7 & IE8

I hope i explained it well what i need. Thanks in advance

A: 

Your code snippet will capture the contents of the textfield at the time the code is executed.

This behaviour is the same on IE7,8 and Firefox.

Make sure your code is only been run on document.ready.

Matt
@Matt: code runs after document.ready. But behavior is not same on IE7,8 and Firefox.
Novice
+1  A: 

There's nothing about that snippet that would be incompatible with any of the IEs. Maybe you have a syntax error elsewhere that's breaking your code? For example, a list or hash defined with a trailing comma is a common IE-only error, if it appears anywhere in a <script> block it breaks the whole thing.

darkporter
+1  A: 

What's txt1? Is it an <input>?

Use input.value, or in jQuery val(), to read the value of a form field. text() reads textual content inside an element, which for most form fields is nothing.

For a <textarea> the textual content will be the initial contents of the field as in the source HTML file, not the current value of the field. (These initial contents correspond to the DOM defaultValue property, not value. In an <input>, this is the value="..." property instead of textual content, but it's still defaultValue in the DOM and not value.)

bobince
txt1 is asp textbox control
Novice
@bobince: I didnt get what are you saying here. There is a difference between these two line of code (IN FIREFOX ONLY) $("#<%=txt1.ClientID%>").attr("value") and $("#<%=txt1.ClientID%>").text()WHEN YOU CHANGE VALUE OF TEXT BOX AT CLIENT SIDE ONLY
Novice
You should not use either of those lines of code. To read a form field value, the correct jQuery method is `val()`. `attr('val')` is misleading (it's actually reading the value *property*, not the `value=""` *attribute*), and `text()` is simply wrong: it'll return the `defaultValue` property for a multi-line text box, and nothing at all for a single line text box, except on IE, where you get the `value` instead of the `defaultValue` due to bugs. Avoid all this confusion: just use `val()`.
bobince
Novice