Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+5 votes
3.6k views
in Q2A Core by
edited by

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
by
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()
by
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
by
selected by
 
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 -->

by
So you had to update core js file in anyway. But this is good to know thanks for sharing Well done..!
0 votes
by

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

by
Okay, good idea. Have not thought of it.

But how do I identify the submitted text of the recent answer? I have to go over it to make some changes. But I cannot go over the other answers as they are already changed by first page load.
by
So you mean you want to do stuffs on the fly while posting an answer?
by
I think you can try with this parameter '$params['text']'
by
As I see the post is done by ajax, so I receive the data back from the server. This is put as HTML into the website, as far as I understand. Now I need to go over the new set HTML to parse this text in the user's browser. (nothing server side)
by
Can you please write few words exactly what you are trying to achieve? May be than can think a bit more toward that.
by
:) of course
1. the user writes his answer, clicks "Add Answer" submit button
2. answer gets submitted by ajax to the server
3. answer is set as HTML (*without* reload of the page, so I cannot use my custom text-converter-function that is normally called after page load)
4. I have to get the new HTML and call my custom text-converter-function on that answer.
The javascript text-converter-function converts maths carets to exponents, http://www.gute-mathe-fragen.de/14157/
by
This means you want to modify answers text on the fly before store into the database?
by
No I do not modify the posted text at all. Server gets the pure version. Just after it comes back from the server, I use JS to change it in the user's browser.

Problem: When the user posts his answer, the page is not reloaded. This is why I have to insert my convert function somewhere.
by
Than I believe $params['text'] should works. Doesn't it gives you the text content?
by
I will try tomorrow, do you know an example plugin that uses it to make dev faster?
by
ah I see,  function process_event($event, $userid, $handle, $cookieid, $params) { ...}
event module.
by
This is what I am searching for you. I think you also can try with jquery if possible to modify content before render. In fact I am also trying to find the solution. If you found please let me know too.
by
The q2a javascript codes are not easy to read and rarely commented. Makes it not that easy. I will try to find a solution tomorrow or day after. Good night :)
by
Yes that is the event I was talking about. At last you find it.  GN :)
by
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
by
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]);";
        }
...