Solved

Toggling button text does not work 1st time

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

832 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