Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

How to trigger javascript function on answer text after submitting the Answer?

+5 votes
1,234 views
asked Feb 12, 2013 in Q2A Core by q2apro
edited Feb 12, 2013 by q2apro

For the answer button I see the following HTML:

<INPUT onClick="qa_ckeditor_a_content.updateElement(); 
return qa_submit_answer(21352, this);" 
VALUE="Add answer" 
TITLE="" TYPE="submit" 
CLASS="qa-form-tall-button qa-form-tall-button-answer">

Checking the javascript files I find function qa_submit_answer(questionid, elem) in qa-question.js

Now I need to know where I can insert my custom javascript function that gets triggered after the answer has been sent, and the answer text is set. I have to access the text from the answer.

Thanks,
Kai

 

PS: When editing the entire load is reloaded. Why not for the answer?

Q2A version: 1.5.4
commented Feb 12, 2013 by q2apro
Now I see that all data is transfered to function qa_ajax_post(operation, params, callback) to make an ajax call. From there it is called back to defined "lines" from qa_submit_answer()
commented Jul 22, 2013 by monk333
Kai, please have a look to the thread here:

http://www.question2answer.org/qa/25975/after-answering-question-with-extra-information-suggestions

May be You can give me some hint how to achieve that (show suggestions or sharebox).

3 Answers

+2 votes
answered Feb 13, 2013 by q2apro
selected Jul 10, 2013 by q2apro
 
Best answer

I solved this finally :)

You open qa-question.js and find line 92:

e.innerHTML=lines.slice(3).join("\n");

e is the on-the-fly-created div that will hold the ajax returned answer html block.

lines.slice(3) is lines[3] that is the ajax returned HTML block!

lines[0] stands for successful ajax return (1 is success)
lines[1] says if the answer button should be hidden
lines[2] is count of answers to be set in HTML
lines[3] is FULL HTML block

Now I am including my custom function:

e.innerHTML = myconverterFunction( lines.slice(3).join("\n") );

done.

 

Note: The lines.slice(3).join("\n") contains ALL html elements to create the answer block, beginning from:
<div class="qa-a-list-item hentry answer" id="a123">
    <form method="POST" action="../555/test">
    ...
    ...
</div> <!-- END qa-a-list-item -->

commented Feb 13, 2013 by jatin.soni
So you had to update core js file in anyway. But this is good to know thanks for sharing Well done..!
0 votes
answered Feb 12, 2013 by jatin.soni

I am not sure but did you try using event module? This may works

using 'a_post'

http://www.question2answer.org/modules.php?module=event

commented Feb 12, 2013 by jatin.soni
Yes that is the event I was talking about. At last you find it.  GN :)
commented Feb 13, 2013 by q2apro
Good morning, well, 1 step further:
- in qa-ajax-answer.php the HTML is sent back by line 98:
$themeclass->a_list_item($a_view);
- all echos before this line are ajax-returned and qa-question.js is getting those returns with the array lines[]
- for function a_list_item() you do not see an echo but the default $this->output(); but as I remember for ajax you can use both "echo" and "return" so this is part of the array!

Bingo!

// slice returns the selected elements in an array, as a new array object
// lines[3] is HTML content, see line:
e.innerHTML=lines.slice(3).join("\n");

Issue here: This HTML block contains ALL html elements to create the answer block, beginning from:
<div class="qa-a-list-item hentry answer" id="a123">
    <form method="POST" action="../555/test">
    ...
    ...
</div> <!-- END qa-a-list-item -->
0 votes
answered Jun 27, 2013 by Tariq
How about modifying the PHP editor module?

For example, if you were using ckeditor, go to the qa-wysiwyg-editor.php and modify this function:

        function update_script($fieldname)
        {
            return "qa_ckeditor_".$fieldname.".updateElement();";
        }

to append your custom function just after the updateElement() function.

One practical example is updating the Mathjax Latex codes. In this case I would make it this way:

        function update_script($fieldname)
        {
            return "qa_ckeditor_".$fieldname.".updateElement();MathJax.Hub.Queue([\"Typeset\",MathJax.Hub]);";
        }
...