I have a function below where the if statement works when it comes to adding content into a single textarea when clicking the “Add” button:
function addwindow(questionText) {
if(window.console) console.log();
if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
$('#mainTextarea').val(questionText);
} else {
$(plusbutton_clicked).parent('td').next('td.question').find('textarea.textAreaQuestion').val(questionText);
}
$.modal.close();
return false;
}
The problem is the else statement is not working when it comes to adding content within one of the appended textareas. What I want is that if a user clicks on a Plus button and adds a content by clicking on the “Add” button, the textarea which belongs in the same row as the plus button which was clicked on will add the content. What do I need to change in order to do this?
Below is the code whch shows the textareas and plus buttons which are appended:
var plusbutton_clicked;
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);
}
Below is the php code where it displays the “QuestionContent” and “Add” button for each row. When user clicks on the “Add” button it adds the “QuestionContent” into the single textarea on top but not in the multiple textareas which are appended.
<?php
$output = "";
while ($questionrow = mysql_fetch_assoc($questionresult)) {
$output .= "
<table>
<tr>
<td class='addtd'><button type='button' class='add' onclick='parent.addwindow();'>Add</button></td>
</tr>";
}
$output .= " </table>";
echo $output;
?>
You can view application here. Please follow the steps to use the application:
- Click on “Add Question” button, then will add a textarea within a new row.
- Click on the “Green Plus” button within the table row you just added, a modal window will appear.
- In modal window it displays a search bar, in search bar type in “AAA” and click on “Search” button
- Results will appear of your search, click on “Add” button to add a row. You will find out modal window is closed but the content from the “Question” field is not added in the textarea within the row you clicked on the green plus button
If you did the steps in the top textarea (the one above the horizontal line), then it does work, it just doesn’t work for textarea which you have just added
One of your errors:
You’re using jQuery 1.7 in your page, the
.livemethod has been deprecated since jQuery1.7. You should use.oninstead.You’re also triggering your
plusbuttonfunction in your generated links:Which isn’t passing any element as parameter to your
plusbuttonfunction be stored in your global varplusbutton_clickedwhich you are expecting. There’s no reference to the clicked element then, therefore it doesn’t find any element when you use theplusbutton_clickedvar in theaddwindowfunction.Either keep the
.onmethod or theonclickcalls above to prevent the function from being fired twice.There might be other errors as well (e.g. You’re checking for id attributes which do not exist for the dynamically generated elements), but this is the main one. Also, I wonder if you don’t have any deadline to finish this, it’s the 10th time I see this question in the previous 3 weeks.. At least you made some progress, congratz.