0

I'm making quote function on my forum.
When user clicks Quote link, I want to add a content from this post into textbox(txtPost) - content is not a problem (I tried alert(content), and it works).
But text in Textbox is not refreshing - alert(txtPost.value) show some added content etc, but textbox is still empty. Why? How to resolve it?

Post:

<asp:HyperLink ID="quotebutton" CssClass="quotebutton" OnClick="javascript:addQuote('somecontent');" runat="server">Quote</asp:HyperLink>

Textbox code:

     <nforum:Emoticons ID="emoticonInclude" runat="server" />
        <div style="width: 604px; margin-left: 198px;">
            <asp:TextBox ID="txtPost" runat="server" TextMode="MultiLine" Rows="14" Width="600"
                ClientIDMode="Static"></asp:TextBox>
        </div>

Javascript function:

function addQuote(content) {
    var txtPost = document.getElementById("txtPost");
    alert(content);
    txtPost.value = txtPost.value + content;
    alert(txtPost.value);
}

INFO!! edit

I'm using tinyMCE editor. Still not refreshing content, while tinyMCE is connected to this textbox. How to do it?

<script type="text/javascript">
    tinyMCE.init({
        // General options
        mode: "exact",
        elements: "txtPost",
        theme: "advanced",
        plugins: "insertcode",
        // Theme options
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,formatselect,|,bullist,numlist,|,link,unlink,insertcode",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "center",
        theme_advanced_resizing: true,
        remove_linebreaks: false,
        relative_urls: false,
        content_css: "/css/nforumeditor.css"
    });

</script>

PROBLEM SOLVED

For tinyMCE-

function addQuote(content) {
    tinyMCE.execCommand('mceInsertContent', false, content); 
    return false;
}
0

3 Answers 3

1

To use server controls in javascript you must call/get the server control's ClientID

html

<asp:HyperLink ID="quotebutton" CssClass="quotebutton" OnClick="javascript:return addQuote('somecontent');" runat="server">Quote</asp:HyperLink>

javascript

function addQuote(content) {
var txtPost = document.getElementById("<%= txtPost.ClientID %>");
txtPost.value = txtPost.value + content;
return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Between Adil and you, you have found the solution. Either you add Adil's solution to your answer or he yours and either one of you gets my +1. :)
0

Tou should return false; in your function and prevent to postback .

Comments

0
<asp:TextBox ID="txtPost" runat="server"></asp:TextBox>
<asp:HyperLink ID="quotebutton" runat="server" onclick="javascript:addQuote('some content');">Quote</asp:HyperLink>

function addQuote(content) {
        var txtPost = document.getElementById('<%= txtPost.ClientID %>');
        txtPost.value = txtPost.value + content;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.