Solved

Toggling button text does not work 1st time

Posted on 2013-02-01
4
197 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now