?
Solved

Toggling button text does not work 1st time

Posted on 2013-02-01
4
Medium Priority
?
237 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

762 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