Solved

Toggling button text does not work 1st time

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

688 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