Ticket #4533 (closed bug: invalid)
text(val) and new String
| Reported by: | kspeakman | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.4 |
| Component: | core | Version: | 1.3.2 |
| Keywords: | text(val) | Cc: | |
| Blocking: | Blocked by: |
Description
text(val) fails if val is a Javascript String object. This is counter-intuitive, and hard to track down. The default error message when this happens is not meaningful. Firefox says "G is undefined". Chrome says "Uncaught TypeError: Cannot read property 'length' of undefined". Safari says "Undefined value". IE says "'length' is null or not an object".
Repro
Repro code follows and is also attached:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<input type="button" value="Crash!" onclick="$('#test').text(new String('Test2'));" />
<span id="test">Test</span>
</body>
</html>
Fix
Suggested fix follows. I haven't looked at the text(val) code, but in my imagination a fix would look something like this:
text: function(val) {
if (val.toString !== undefined) val = val.toString();
...
}
Attachments
Change History
Changed 4 years ago by kspeakman
-
attachment
bug.jQuery.text.html
added
comment:2 Changed 3 years ago by miketaylr
- Status changed from new to closed
- Resolution set to invalid
.text() takes a string primitive as an argument ( http://api.jquery.com/text/), rather than a string object.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Demo page for the text(val) and Javascript String object bug.