Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.4k views
in Q2A Core by
edited by
Hi,
I want to add an "onclick" event on a span/table row of Question-content. I couldn't found a way to view/edit HTML code of question written in ckeditor.
 
Then, I tested, if putting an html text with "onclick" event on div/span/tr directly from database, is working. But, when I view the question in browser, it seems "onclick" event seems to be omitted in final output.
 
for e.g., i added following in the content column of qa_posts (and I have already set format column of qa_posts to html) - 
<span id="test" class="test" onclick="alert('hello');">This is a Test Question.</span>
 
In normal html file, it returns an alert box when clicked; but when I tested it in Q2A, onclick did not trigger. So, I checked the source code for that question and it was -
<span id="test" class="test" >This is a Test Question.</span>
 
The onlick event was removed while displaying, but its present in database.
 
Why is that? and how to add it?
Thanks.
P.S. - I am not talking about Admin -> Layout -> "Custom HTML <location>:' Javascript, there is working fine.
Q2A version: latest

1 Answer

+1 vote
by
selected by
 
Best answer

The posts get filtered by htmlawed. Script tags get removed. onclick as well.

Even when sending the post from DB to frontend, the filtering takes place!

For your case: You can use an id (or class), then use jquery to trigger the desired behaviour. Put the jquery code into the file qa-content/qa-page.js

Or put the code, as you wrote yourself, into the Custom HTML section.

Hope that helps,
Kai

by
Thanks for the reply. I could also reverse trace the code to this line of htmLawed.php (line 40)->
$x = array_flip((isset($x[0]) && $x[0] == '*') ? explode('-', $x) : explode(',', $x. (!empty($C['safe']) ? ',on*' : '')));
if I change ",on*" to '' all events work properly.
I have basic knowledge of PHP and this line is very complex for me.
For the better understanding, can you please tell me what does this line actually do.
Thanks.
by
Actually you should not hack the core files as with every update you will have extra work to do, plus behaviour could change. Try the jquery implementation, you can also insert jquery code into the theme file qa-theme.php (page.js is also not update safe...)
by
I got your point. I did this before your answer. Though took me a whole day to reverse trace.
I will now try your way.
I am asking so that I can understand that line.
Thanks.
...