Skip to main content

Bug Tracker

Side navigation

#1954 closed bug (fixed)

Opened November 22, 2007 04:04AM UTC

Closed April 17, 2011 10:20PM UTC

Last modified March 09, 2012 04:47AM UTC

val() returns innerHTML for button elements in IE

Reported by: dmuir Owned by: timmywil
Priority: high Milestone: 1.6
Component: manipulation Version: 1.4.4
Keywords: IE button val() Cc:
Blocked by: Blocking:
Description

val() returns the value of the value property in FF, but returns the value of innerHTML in IE.

jQuery 1.1.4 did not have this bug.

html:

<button value="myValue">Click Me</button>

js:

alert($('button').val());

IE: alerts "Click Me"

FF: alerts "myValue"

Attachments (1)
Change History (25)

Changed November 22, 2007 04:10AM UTC by dmuir comment:1

using .attr('value') does not work around this issue either

Changed November 22, 2007 05:03AM UTC by dmuir comment:2

must have been seeing things... tested again with 1.1.4 and it doesn't work there either.

The only solution I have is to retrieve the html for the button element and do a regex to extract the value attribute. IE's button dom element does not have a property storing the value (it is overwritten by the button's innerHTML).

Changed November 28, 2007 08:02AM UTC by davidserduke comment:3

Weird. I see the same thing you do. Using the debugger the "value" as assigned by the attribute just isn't stored anywhere and appears to be overwritten by the innerHTML. It's interesting if you overwrite the value it will then be correct like:

$("button").val("newValue");
alert($("button").val());

alerts "newValue" in both IE and FF. I hate to say it but there does not appear to be an easy cross-browser fix. :(

Changed January 14, 2008 07:21PM UTC by john comment:4

resolution: → wontfix
status: newclosed

Doesn't look like this can be resolved, unfortunately.

Changed October 17, 2008 10:50AM UTC by luka.kladari comment:5

resolution: wontfix
status: closedreopened

i've dug up a fairly dirty/ugly workaround (credit: http://odyniec.net/projects/iefixbuttons/):

var rawval = $("button").get(0).outerHTML.match(/value\\s*=\\s*['"](['"]*)['"]/);

var val = (rawval ? rawval[1] : );

to be combined with if($.browser.msie)

this will fail in cases like value="bla'bla'bla", which are perfectly valid in HTML

can someone with better JS regex skills than me figure out how to catch the opening character and match everything until it shows up again? I believe that would be a fairly robust (albeit ugly) fix, which could be implemented in jquery core?

Changed October 17, 2008 02:24PM UTC by flesler comment:6

milestone: 1.2.21.3
need: ReviewPatch
owner: → flesler
status: reopenednew

aButton.attributes.value.nodeValue returns the value on IE.

I think this can be indeed worked around.

Changed October 17, 2008 02:32PM UTC by flesler comment:7

status: newassigned

Changed October 17, 2008 02:32PM UTC by flesler comment:8

cc: → dmuir

Changed November 25, 2008 11:59AM UTC by jollytoad comment:9

Can also use:

buttonElement.getAttributeNode('value').value

Changed May 06, 2009 03:00AM UTC by dmethvin comment:10

Bug #3259 has a test case and link to further discussion.

Changed June 20, 2010 05:00PM UTC by dmethvin comment:11

resolution: → worksforme
status: assignedclosed

This seems to be working now, tested in 1.4.2 and IE8.

Changed November 04, 2010 12:30AM UTC by Anthony Johnston comment:12

Replying to [comment:13 dmethvin]:

This seems to be working now, tested in 1.4.2 and IE8.

doesn't work in ie7 or wp7 (which is also ie7)

comment 6 has the answer

Changed December 23, 2010 01:39PM UTC by anonymous comment:13

As stated above this issue is not fixed yet for IE7 in jQuery 1.4.4. The workaround mentioned in comment:6 works for IE7 on WinXP and should be implemented for the val and attr functions.

Unfortunately, this issue tracker in use here doesn't permit to reopen the bug for whatever reason.

Changed January 10, 2011 03:07PM UTC by jitter comment:14

cc: dmuir
component: coremanipulation
milestone: 1.31.6
priority: majorhigh
resolution: worksforme
status: closedreopened
version: 1.2.11.4.4

Changed January 10, 2011 03:07PM UTC by jitter comment:15

status: reopenedopen

Changed January 10, 2011 03:13PM UTC by jitter comment:16

#7938 is a duplicate of this ticket.

Changed January 11, 2011 03:45PM UTC by robert.clark comment:17

I have an updated patch against git that handles reading & setting button values via .val() & .attr('value') as well as .removeAttr('value'). I don't seem to be able to attach it to this ticket, though. Shouldn't there be an "Attach file" button somewhere?

Changed January 12, 2011 12:54AM UTC by dmuir comment:18

can't see where to add an attachment either... but I'm curious to see what your solution is.

Changed January 12, 2011 01:01PM UTC by robert.clark comment:19

It's nothing especially clever, just some special-cases that use getAttributeNode, setAttributeNode & removeAttributeNode. The special-cases target only the value attribute on buttons in IE7 (and lower) to minimise the impact.

I've stuck it up here: https://gist.github.com/776092

Changed February 15, 2011 01:33PM UTC by julien.garand+jquery@go-on-web.com comment:20

Hi,

Please consider the following test case with IE7 : http://jsbin.com/ipitu4

Each button is written as

<button value="some value">some code</button>

.

We would expect IE7 to return the correct "value" attribute value when calling

$('button').attr('value');  // BUG

... It doesn't !

The solution is given by the third button

$('button').get(0).getAttributeNode('value').value;   //CORRECT

Regards,

Julien

Changed February 15, 2011 01:38PM UTC by anonymous comment:21

Replying to [comment:22 julien.garand+jquery@…]:

Same test case with jsFiddle : http://jsfiddle.net/sKuHr/1/

Changed February 27, 2011 12:25AM UTC by robert.clark comment:22

Here's a new patch which tests for the bug instead of using browser detection:

https://gist.github.com/845759

Changed February 27, 2011 01:10AM UTC by robert.clark comment:23

And another, stripped-down, patch which only fixes the most common usage case (reading the value attribute):

https://gist.github.com/845799

Changed April 17, 2011 09:15PM UTC by john comment:24

owner: fleslertimmywil
status: openassigned

Changed April 17, 2011 10:20PM UTC by timmywil comment:25

resolution: → fixed
status: assignedclosed

Fix value attribute and val for value on button elements. Fixes #1954

Changeset: 34d80709ce9bd583f6f5dd17e9bffa2fb7cff3c5