Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  73] [ 3]  / answers: 1 / hits: 46636  / 10 Years ago, fri, october 24, 2014, 12:00:00

I'm trying to write text into an element span with function .text(), but I got this error
UncaughtTypeError: undefined is not a function, and I also tried function append(), innerHtml(), createTextNode() with no success.



What am I doing wrong?



var closeSpan = document.createElement(span);
closeSpan.setAttribute(class,sr-only);
closeSpan.text(Close); //UncaughtTypeError: undefined is not a function


OR



var closeSpan = document.createElement(span);
closeSpan.setAttribute(class,sr-only);
closeSpan.append(Close); //UncaughtTypeError: undefined is not a function

More From » jquery

 Answers
10

Since you are starting with pure DOM code, I'd suggest continuing with pure DOM code:



var closeSpan = document.createElement(span);
closeSpan.setAttribute(class,sr-only);
closeSpan.textContent = Close;


In other words, just set textContent to the value you want.



If compatibility with IE 8 or earlier matters to you, note that textContent does not exist for those older browsers. For those older ones you'd have to use innerText (which works on IE 8 but is not part of any standard) or innerHTML. See the MDN page on textContent (which I link to above) for a discussion of the differences between these fields.


[#69026] Tuesday, October 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;