Joshkunz
Joshkunz

Reputation: 6045

jQuery .text() and .innerHTML not working in IE

I'm trying to add some text to some spans within a div pretty simple but I cannot for the life of me get this to work in IE. It works in Chrome, Safari, and Firefox.

heres the jQuery:

$("div#dateRanges #startDate").text(typeValues.dateRanges[1][0]);
$("div#dateRanges #endDate").text(typeValues.dateRanges[1][1]);

and heres the bit of html I'm editing:

<div id="dateRanges">
    <span id="startDate"></span>
    <span id="endDate"></span>
</div>

using jQuery to get the element and then trying to set the .innerHTMl also didn't work.

var startDate = $("div#dateRanges #startDate").first();
startDate.innerHTML = typeValues.dateRanges[1][0]; //Sets the text date values
var endDate = $("div#dateRanges #endDate").first();
endDate.innerHTML = typeValues.dateRanges[1][1];enter code here

Just to clear up potential confusion it's the setting the text of the spans I'm having trouble with. Also it may be important to include the information that this data is being updated after the page loads, through AJAX.

Upvotes: 1

Views: 2486

Answers (3)

PapaHotelPapa
PapaHotelPapa

Reputation: 697

Apparently the reason for this is that jQuery's $('#foo') method returns an object rather than an element. There's a fascinating short write-up about it here: http://markmail.org/message/nj3iclk6jnczshop

To get around that problem, you can use the method suggested in that article, I'd imagine, but the way that I did it was to just use jQuery's .html() method with no arguments, as suggested by the write-up as well.

Upvotes: 0

Zain Shaikh
Zain Shaikh

Reputation: 6043

try following code, and make sure typeValues.dateRanges is not null.

var startDate = $("div#dateRanges #startDate");
startDate.html(typeValues.dateRanges[1][0]); //Sets the text date values
var endDate = $("div#dateRanges #endDate");
endDate.html(typeValues.dateRanges[1][1]); //enter code here

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

For the second, you need to get the raw DOM element. You can do this with [0] or .get(0)

var startDate = $("div#dateRanges #startDate")[0];
var endDate = $("div#dateRanges #endDate")[0];

Probably, text works, but you want it to instead be treated as HTML. I think you can use html instead here:

$("div#dateRanges #startDate").html(typeValues.dateRanges[1][0]);
$("div#dateRanges #endDate").html(typeValues.dateRanges[1][1]);

Upvotes: 1

Related Questions