1

This is driving me crazy - basically this if statement is not working. The code keeps jumping into the else statement, I have hovered over the buttonText and everything seems alrite until it hits the conditions.

function DesignCodeViewClick(el) {

  $("div.divdesigncodeviewbuttonsselected").attr("class", "divdesigncodeviewbuttons");
  $(el).attr("class", "divdesigncodeviewbuttonsselected");

  var buttonText = el.innerText;

  if (buttonText.toLowerCase() == "design") {
      $("#iframecms").css("display", "none");
      $("textarea.divhtmleditor").css("display", "none");
  }
  else if (buttonText.toLowerCase() == "browse") {
      $("#iframecms").css("display", "block");
      $("textarea.divhtmleditor").css("display", "none");
  }
else {

      $("textarea.divhtmleditor").css("display", "block");
      $("#iframecms").css("display", "none");
      WebForm_DoCallback('SEOCMSControl1', 'getcode~http://www.triksportfolio.com', GetCodeServerResponse, null, null, true);

  }

}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
redoc01
  • 2,107
  • 5
  • 32
  • 64
  • What is the HTML? It could be that there are white space characters in the text such as new lines or spaces. – Felix Kling May 02 '11 at 11:34
  • did you debug the value of buttonText or el.innerText? – Naren Sisodiya May 02 '11 at 11:36
  • 3
    You probably should use ".addClass()" and ".removeClass()" instead of ".attr()" to manipulate element classes. – Pointy May 02 '11 at 11:37
  • If you are dealing with buttons, IE thinks the text content and the value are the same thing. jQuery's `attr()` method doesn't fix that, you may need to use the W3C `getAttributeNode()` method. – RobG May 02 '11 at 14:00

1 Answers1

4

textContent is the standard property. innerText is a Internet Explorer thing I believe. They are almost the same, but not quite.

If you did want to do it that way, I'd suggest this...

var text;
if ('textContent' in el) {
   text = el.textContent;
} else {
   text = el.innerText;
}

But, you are using jQuery, however, so just use text() method.

To ensure there is no leading or trailing whitespace, you can use jQuery's trim() utility function.

In addition, you are doing a few things jQuery can help you with. Look at addClass() and hide().

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • @alex It exists two Methods: .innerText and .innerHTML. FF, Safari and Chrome in example supports these tags. 'text()' uses the same way as above. – Reporter May 02 '11 at 11:38
  • @reporter I'm sorry, I'm not sure what your comment is about. – alex May 02 '11 at 11:39
  • @alex innerText is the original Method. Ms has invented it, but all Browsers supports that. But you're right. for a clean code tthe op should use the jqery method – Reporter May 02 '11 at 11:42
  • +1 shorter plain JS version: `var text = el.innerText || el.textContent`. – Felix Kling May 02 '11 at 11:49
  • @reporter - read that "not quite" link Alex included in his answer. – Pointy May 02 '11 at 11:49
  • @alex Hm, jQuery is available. Why not just: `$(el).text()`? – Šime Vidas May 02 '11 at 11:50
  • @Felix Don't know why I didn't think of that! Next time :) – alex May 02 '11 at 11:52
  • @Pointy OOps I did oversee the almost invsible link. – Reporter May 02 '11 at 11:55
  • @reporter - not all browsers support `innerText`, and jQuery's `text()` method doesn't use innerHTML or innerText (or textContent), it uses recursion to traverse DOM nodes and concatenates the values of the text nodes it encounters. – RobG May 02 '11 at 13:52