0

I have (successfully) been using the following on my old Vista machine:

var isIE = isInternetExplorer();

$("#divTablesButton" + (isIE ? "" : " :input"))
    .attr( "disabled", this.m_Array1.length === this.m_Array2.length ? true : false );

where divTablesButton is a button and isInternetExplorer is the function (which still seems to be working fine):

function isInternetExplorer() {
    return ( getInternetExplorerVersion() < 0 ? false : true );
};

function getInternetExplorerVersion() {
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
};

But now I have noticed this does not work on my Win 8.1 machine with the newest versions of IE and Chrome.

Anyone know what is up? To clarify, it's the attr("disabled" bit that is wrong. The buttons do not disable.

Sampson
  • 265,109
  • 74
  • 539
  • 565
Rewind
  • 2,554
  • 3
  • 30
  • 56
  • 1
    .is(":disabled") comment: Surely that is asking if it is disabled rather than just setting it. (I have tried it and it returns false before and after my attempt to disable the button.) – Rewind Nov 18 '14 at 18:16
  • since its returning false your selector is `#divTablesButton :input` do you really have an input element inside your button? Which one are you trying to disable the button or the input? Also you do not need the ternary stuff for the length comparison as the comparison itself will return a boolean value – Patrick Evans Nov 18 '14 at 18:20
  • 1
    Understand, I forget that one @Rewind. Can you try $('element').prop('disabled', true); – HaveNoDisplayName Nov 18 '14 at 18:22
  • IE11 now shows 'Netscape' instead of 'Microsoft Internet Explorer'. See http://stackoverflow.com/questions/17907445/how-to-detect-ie11 – Stryner Nov 18 '14 at 18:29
  • Why are you treating modern versions of Internet Explorer differently than Chrome? What problems are you attempting to solve with this script? – Sampson Nov 19 '14 at 00:05

1 Answers1

1

What version of jQuery are you using? In versions >1.6 they want people to use .prop() instead of .attr() for properties like disabled, required, checked etc. (things that are booleans). This older SO question will explain better than I can.

Try using prop() instead like below. Additionally, there's a piece of your code that you can remove. You've written a statement variable_a === variable_b ? true : false. The result of variable_a === variable_b will always be true or false, so you don't need the ? true : false part.

var isIE = isInternetExplorer();
$("#divTablesButton" + (isIE ? "" : " :input")).prop("disabled", this.m_Array1.length === this.m_Array2.length);

Hope this helps.

Community
  • 1
  • 1
grammar
  • 871
  • 10
  • 22
  • thats the same I suggest in comment section – HaveNoDisplayName Nov 18 '14 at 18:25
  • Ha! your comment came in while I was writing my answer. – grammar Nov 18 '14 at 18:26
  • True!! no hard feelings @grammar – HaveNoDisplayName Nov 18 '14 at 18:26
  • Haha I know - hate when that happens. It's a race :D – grammar Nov 18 '14 at 18:27
  • Wow, thanks for all the tips. It is now working. I can remember I needed to add " :input" for chrome before, but that is obviously not the case for buttons. So when do I need to add input? – Rewind Nov 18 '14 at 19:19
  • You shouldn't need to add `:input` anywhere anymore. I'm not sure why you would have needed it in the first place actually, but it depends on what `#divTablesButton` is. Is it a ` – grammar Nov 18 '14 at 19:36