[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Toggling button text does not work 1st time

Posted on 2013-02-01
4
Medium Priority
?
242 Views
Last Modified: 2013-02-06
I'm replacing a text_area with the ability to toggle back and forth with a rich text editor. Everything works fine except the button text does not toggle the first time. I've seen this once before but can't recall how I resolved it.

$rich_text value is extracted from database first.

    <script type="text/javascript">
      var posting_nice_div;
      var rich_text;
      
      $(document).ready(function() {

        <?php if ($rich_text): ?>
          posting_nice_div = new nicEditor({buttonList: ['fontSize','fontFamily','bold','italic','underline','left','center','right','justify',
                                              'ol','ul','subscript','superscript','strikethrough','removeformat',
                                              'indent','outdent','hr','image','forecolor','bgcolor','link','unlink'
                                              ],
                                 iconsPath : 'js/nicEditorIcons.gif'}).panelInstance('posting_text');
          rich_text = true;
        <?php else: ?>
          rich_text = false;
        <?php endif; ?>

      });

      function toggle_edit (buttonObj) {
        if (!rich_text) {
          posting_nice_div = new nicEditor({buttonList: ['fontSize','fontFamily','bold','italic','underline','left','center','right','justify',
                                              'ol','ul','subscript','superscript','strikethrough','removeformat',
                                              'indent','outdent','hr','image','forecolor','bgcolor','link','unlink'
                                              ],
                                 iconsPath : 'js/nicEditorIcons.gif'}).panelInstance('posting_text');
          rich_text = true;
          buttonObj.innerHTML = "Rich formatting >>";
          
          $.post ("ajax/rich_text.php", { pos: "<?php echo $posNum; ?>", rich: rich_text },
                  function( data ) {
                    alert(data);      
                  })
          
        } else {
          posting_nice_div.removeInstance('posting_text');
          rich_text = false;
          buttonObj.innerHTML = "<< Plain text";

          $.post ("ajax/rich_text.php", { pos: "<?php echo $posNum; ?>", rich: rich_text },
                  function( data ) {
                    alert(data);      
                  })
          
        }
        
      }
    </script>
    

        <button id="toggle_button" type="button" class="float_right ui-button small_btn" onClick="toggle_edit(this); return false"><?php if (!$rich_text) echo 'Rich formatting >>'; else echo '<< Plain text'; ?></button>

        <textarea id="posting_text" rows="15" name="Listing"><?php echo $listing; ?></textarea>

Open in new window


First time the button is clicked the button text does not change. For each successive click it changes just fine.
0
Comment
Question by:jimdgar2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38845242
top of your script put :

<?php echo $rich_text; ?>

to be sure it's set to true or false at the beggining so you don't start with a wrong value for a wrong state (rich text instead norma)
0
 

Author Comment

by:jimdgar2
ID: 38845314
leakim971:

Nope, I've already checked that. I've also stepped through the js code. It's as if the first time the function is executed that line 28 or 38 (above) are simply not executed. The 2nd through nth executions they are.
0
 

Accepted Solution

by:
jimdgar2 earned 0 total points
ID: 38845878
Doh! My stupid mistake; reversed the logic.
0
 

Author Closing Comment

by:jimdgar2
ID: 38858750
Solved myself.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question