-1

Need use javascript get a asp.net label text, below javascript works fine for IE and Chrome, but not Firefox, how to make it work for all browsers.

function showThumbnails_OnClick() {

        var id = document.getElementById('lblId').innerText;

        if (ChkBox.checked) {
            location.href = 'Result.aspx?Id=' + id;
        }

    }

<asp:Label ID="lblId" runat="server" Text="">
seagull
  • 237
  • 1
  • 7
  • 19

3 Answers3

2

Change .innerText to .textContent

Rohit
  • 300
  • 1
  • 11
  • textContent is not equivalent to innerHTML (eg newlines) see clubajax.org/plain-text-vs-innertext-vs-textcontent – Jim W Oct 16 '13 at 18:11
  • 2
    It's not but, it does solve the problem that the OP is having. – Rohit Oct 16 '13 at 18:23
1

you will have to write a function that handle both cases:

function showThumbnails_OnClick() {
    var element = document.getElementById('lblId');
    var id = element.innerText || element.textContent;

    if (ChkBox.checked) {
        location.href = 'Result.aspx?Id=' + id;
    }

}
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • textContent is not equivalent to innerHTML (eg newlines) see http://clubajax.org/plain-text-vs-innertext-vs-textcontent/ – Jim W Oct 16 '13 at 18:10
  • 1
    @JimW yes it is not. never said it was. it only returns the text without any html in it. Maybe that's exactly what the op needs – Ibu Oct 16 '13 at 18:13
  • @JimW This solution solves the OP's problem. Just because textContent and innerHTML aren't equivalent doesn't mean that you downvote the answers that solve the problem. – Rohit Oct 16 '13 at 18:27
  • 1
    @Rohit my bad, you're right. – Jim W Oct 16 '13 at 19:54
1

To make sure it will work on IE and FF.

var c_id = document.getElementById("lblId");
var id = (c_id.textContent == undefined) ? c_id.innerText : c_id.textContent;
A Ghazal
  • 2,693
  • 1
  • 19
  • 12