admin管理员组

文章数量:1323716

Modifying a jQuery plugin, I have lines of code as follows:

var currentread = (self).find('.c'+a).html();
//currentread = currentread.toUpperCase();
$('#output').html(currentread);

With the middle line mented out, the code works fine. The contents of the div with the class 'c[1-n]' is displayed in #output. Unmenting the middle line to convert this to upper case, however, generates the error 'currentread is null' for that line.

I must be doing something stupid here, but this doesn't seem to make sense. Any idea?

Modifying a jQuery plugin, I have lines of code as follows:

var currentread = (self).find('.c'+a).html();
//currentread = currentread.toUpperCase();
$('#output').html(currentread);

With the middle line mented out, the code works fine. The contents of the div with the class 'c[1-n]' is displayed in #output. Unmenting the middle line to convert this to upper case, however, generates the error 'currentread is null' for that line.

I must be doing something stupid here, but this doesn't seem to make sense. Any idea?

Share Improve this question asked Dec 30, 2011 at 19:33 DanDan 1,1912 gold badges23 silver badges39 bronze badges 4
  • 3 Why are you converting html to upper case? Did you mean to use text()? – josh.trow Commented Dec 30, 2011 at 19:35
  • 1 the variable cannot "bee null" when you call .toUpperCase() - it must have been null beforehand. – Alnitak Commented Dec 30, 2011 at 19:37
  • Post you html too. jsfiddle – abuduba Commented Dec 30, 2011 at 19:40
  • josh.trow: Ah, changing it out to text() did the trick. I did think it a bit odd that the original plugin author chose html() but honestly I'm new to jQuery so didn't know about text(). Thanks! – Dan Commented Dec 30, 2011 at 19:40
Add a ment  | 

1 Answer 1

Reset to default 9

Is is simply the case that currentread is null? You could change it to:

currentread = (currentread || "").toUpperCase()

so that toUpperCase is always called on a string.

本文标签: jqueryJavaScript variable becomes null when toUpperCase() appliedStack Overflow