&nbsp not being read by Safari and Opera in JS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    &nbsp not being read by Safari and Opera in JS

    I have to toggle the name of a hyperlink from Edit to Save and vice-versa on click event of the same hyperlink.

    This is the HTML part,

    [HTML]<input class="text" id="ix1" type=text></input>
    <a class="button" id="ax1" href="javascrip t:void(0)" onclick="edit(' x1')">&nbsp;Edi t&nbsp;</a>[/HTML]



    and function edit(id) is
    Code:
          function edit(id) {
          var anc = document.getElementById('a'+id);
          var inp = document.getElementById('i'+id);
          anc.innerHTML = anc.innerHTML=="&nbsp;Edit&nbsp;" ? "&nbsp;Save&nbsp;" : "&nbsp;Edit&nbsp;";
          inp.className = inp.className=="text" ? "edit" : "text";
    }
    The code in bottom line is working fine with all browsers, ie. class name of the input box gets changed on hyperlink's click.
    But the name-change of the hyperlink is working fine with Firefox and IE only, ie, the name of the hyperlink gets changed from ' Save ' to ' Edit ' and ' Edit ' to ' Save ' on every click. But Opera and Safari are not doing any change.
    If I remove the &nbsp;, then its working fine with all.
    But since its a button kind of look, so I need to give space on both sides of the name.

    I know I can solve it with this change...
    Code:
    anc.innerHTML = inp.className=="text" ? "&nbsp;Save&nbsp;" : "&nbsp;Edit&nbsp;";
    but what's the problem with reading &nbsp;?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Do spaces work in place of &nbsp;?

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by acoder
      Do spaces work in place of &nbsp;?
      Toggle thing is working fine, but spaces are not being shown. Thats why I used &nbsp;

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The unicode replacement, \u00a0, should work.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by acoder
          The unicode replacement, \u00a0, should work.
          Doesn't work with Opera and Safari.

          Well don't worry about that, I solved it with other way out.

          Thanks for trying. :)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            It should do. Does this not work? [code=javascript]anc.innerHTML = anc.innerHTML== "\u00a0Edit\u00 a0" ? "&nbsp;Save&nbs p;" : "&nbsp;Edit&nbs p;";[/code]What did you use in the end? I assume it must involve matching "Edit" instead of comparing the string.

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by acoder
              It should do. Does this not work? [code=javascript]anc.innerHTML = anc.innerHTML== "\u00a0Edit\u00 a0" ? "&nbsp;Save&nbs p;" : "&nbsp;Edit&nbs p;";[/code]What did you use in the end? I assume it must involve matching "Edit" instead of comparing the string.

              Ok, I guess there is some difference the way browsers interpret &nbsp;

              Coz' [code=javascript]anc.innerHTML = anc.innerHTML== "\u00a0Edit\u00 a0" ? "&nbsp;Save&nbs p;" : "&nbsp;Edit&nbs p;";[/code] works with Opera and Safari and not Mozilla and IE, while [code=javascript]anc.innerHTML = anc.innerHTML== "&nbsp;Edit&nbs p;" ? "&nbsp;Save&nbs p;" : "&nbsp;Edit&nbs p;";[/code] works fine with Mozilla and IE and not Opera and Safari.

              PS: Earlier, in last post, I said it doesn't work with Safari and Opera. That time I was trying [code=javascript]anc.innerHTML = anc.innerHTML== "\u00a0Edit\u00 a0" ? "\u00a0Save\u00 a0" : "\u00a0Edit\u00 a0"[/code]

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                I think your best bet would be something like:[code=javascript]anc.innerHTML = anc.innerHTML.m atch("Edit") ? "&nbsp;Save&nbs p;" : "&nbsp;Edit&nbs p;";[/code]

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by acoder
                  I think your best bet would be something like:[code=javascript]anc.innerHTML = anc.innerHTML.m atch("Edit") ? "&nbsp;Save&nbs p;" : "&nbsp;Edit&nbs p;";[/code]
                  Yeh, that works!

                  Thanks :)

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    No problem, you're welcome!

                    Comment

                    Working...