Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-28-2012, 03:55 AM   PM User | #1
carlbrooks
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
carlbrooks is an unknown quantity at this point
How to add a content into the correct textarea by clicking on an “Add” button

I have a simple application here which you can access.

When you open the application can you please click on the "Add Question" twice so it adds 2 table rows.

Now you will see in the application that above the horizontal line there is a textarea and a plus button, and below the horizontal line shows 2 table rows, both rows displaying its own textarea and plus button.

Now my question is that I want these 2 outcomes to happen but I do need help from somebody who is good at using Jquery/Javascript in order to solve this situation:

Situation 1. If the user clicks on the plus button ABOVE the horizontal line, then it displays a modal window which contains a search bar, Please type in search bar "AAA". You will now see a list of results. Now what I want is that if the user selects a row by clicking on the "Add" button, then I want the "QuestionContnet" within that row to be displayed in the textarea above the horizontal line. At the moment the Add button just closes the modal window but doesn't add anything into the textarea.

Situation 2: This deals with the user clicking on a plus button within one of the table rows BELOW the horizontal line. I want the same thing to happen except the "QuestionContent" added is displayed in the textarea within the same row the user has clicked the plus button, no where else.

How can both situations be solved so that it adds the QuestionContent into the correct textareas depending on which plus button is clicked?

Below is the code for the application:

Code:
 <head>
        
        <script type="text/javascript">
              
        function insertQuestion(form) {
        
        
        	var $tbody = $('#qandatbl > tbody'); 
            var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
            var $plusrow = $("<td class='plusrow'></td>");
            var $question = $("<td class='question'></td>");
            
            
            $('.questionTextArea').each( function() {
            
        	var $this = $(this);
            var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
                           .attr('value',$this.val());
        
            $question.append($questionText);
            
            });
            
                $('.plusimage').each( function() {
            
        	var $this = $(this);
            var $plusimagerow = $("<a onclick='return plusbutton();'><img src='Images/plussign.jpg' width='30' height='30' alt='Look Up Previous Question' class='imageplus'/></a>").attr('name',$this.attr('name')+"[]")
                           .attr('value',$this.val());
        
            $plusrow.append($plusimagerow);
            
            });
            
            $tr.append($plusrow);
            $tr.append($question);
            $tbody.append($tr);	
            
            form.questionText.value = "";
            
            $('.questionTextArea').val('');
        
        	        	        
        }
        
        function plusbutton() { 
            // Display an external page using an iframe 
            var src = "previousquestions.php"; 
            $.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
            return false;
        } 
        
        
        function closewindow() {     
        	
            $.modal.close(); 
            return false;
        } 
        
    $('.add').on('click', function(){ 
      //lets get our Question Text... 
      var theQuestion = $("td:first-child", $(this).parent()).text(); 
     
      //the row is present, let's then make sure that the proper cell gets oru data. 
      if($('.activePlusRow').length > 0){ 
     
        $('.activePlusRow').next('.textAreaQuestion').val(theQuestion); 
        $('.activePlusRow').removeClass('activePlusRow'); 
        
      } 
      
        $.modal.close(); 
    }); 
    
    
     $('.plusrow').on('click', function(){ 
    //adding a unique class for the purpose of the click. 
    
       $(this).addClass('activePlusRow'); 
     });
    
        
        
        </script>
        
        </head>
        
        <body>
        
        <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">
        
        <div id="detailsBlock">
        <table id="question">
        <tr>
            <td rowspan="3">Question:</td> 
            <td rowspan="3">
                <textarea class="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
            </td>
        </tr>
        </table>
        
        <table id="plus" align="center">
        <tr>
        <th>
        <a onclick="return plusbutton();">
        <img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" name="plusbuttonrow"/>
        </a>
        <span id="plussignmsg">(Click Plus Sign to look <br/> up Previous Questions)</span>
        </th>
        </tr>
        </table>
        
        <table id="questionBtn" align="center">
        <tr>
        <th>
        <input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
        </th>
        </tr>
        </table>
        
        </div>
        <hr/>
        
        <div id="details">
        <table id="qandatbl" align="center">
        <thead>
        <tr>
            <th class="plusrow"></th>
            <th class="question">Question</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
        </table>
        </div>
        
        </form>
        
        </body>
The details stored in the modal window comes from a seperate script known as "previousquestions.php", Below is the code where it shows the result of the "QuestionContent" field only displayed and it's "Add" button after the user has compiled a search:

Code:
<?php 
          
          $output = "";
    $output .= "
        <table border='1' id='resulttbl'>
          <tr>
          <th id='questionth'>Question</th>
          </tr>
    ";
            while ($questionrow = mysql_fetch_assoc($questionresult)) {
    $output .= "
          <tr>
          <td id='questiontd'>{$questionrow['QuestionContent']}</td>
          <td id='addtd'><button type='button' class='add'>Add</button></td>
          </tr>";
            }
            $output .= "        </table>";
            
            echo $output;
    
      }
      
    }
    
    ?>
Thank you
carlbrooks is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, javascript, jquey, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:32 AM.


Advertisement
Log in to turn off these ads.