whoah
whoah

Reputation: 4443

Adding text into textbox by javascript

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;
}

Upvotes: 0

Views: 3697

Answers (3)

tariq
tariq

Reputation: 2258

<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;
    }

Upvotes: 0

Ravinder Gujiri
Ravinder Gujiri

Reputation: 1514

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;
}

Upvotes: 1

user1968030
user1968030

Reputation:

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

Upvotes: 0

Related Questions