Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So, I have this jquery function.
<script type="text/javascript">
      $(document).ready(function () {
          $('#answ').hide();

          $("#btnsubmit").click(function () {
              var tbans = $('<%= txtMain.ClientID %>').val();
              alert(tbans);

              $('#answ').toggle();
              return false;
          });
      });
 </script>


<textarea id="txtMain"  runat="server" rows="20" cols="150" ></textarea>

<input type="submit" id="btnsubmit"  />


Now, when I click on submit button, instead of showing the text written in textarea it shows alert that says,"undefined". Toggle function works though.

What I am missing?

P.s I am using tinymce texteditor plugin with my textarea.

UPDATE 1:
I tried using asp button like this.
<asp:Button ID="btnsubmit" runat="server" Text="Submit" Font-Bold="True" 
           CssClass="style10" BackColor="Gainsboro" Font-Size="Medium" 
            ToolTip="Submit Answer." UseSubmitBehavior="false" /><br />


then it shows the value of textarea in alert on click of Submit button and toggle works. But then page refreshes and toggle disappears. Also, if I remove UseSubmitBehavior="false" from button then page doesnt refresh but again, it doesnt show me the value of textarea.

UPDATE 2: I am using tinyMce texteditor with my textarea. tinymce is a well known texteditor. I checked removing tinymce and then code worked. But thing is, the code even worked with tinymce when I took asp.net button with UseSubmitBehavior="false", just it refreshed the page after clicking submit button.
Posted
Updated 10-Jan-14 0:14am
v3

you have missed "#" in the selector
var tbans = $('<%= txtMain.ClientID %>').val();
try this..
var tbans = $('#<%= txtMain.ClientID %>').val();
   
Comments
JL_Coder 10-Jan-14 4:28am
   
Sorry. I tried writing #. But then "undefined" error is gone, but it is not showing the text written in textarea, it just shows a blank alert.
Karthik_Mahalingam 10-Jan-14 4:36am
   
It should work,
post your latest code....
JL_Coder 10-Jan-14 4:41am
   
please check update in my question.
Karthik_Mahalingam 10-Jan-14 4:46am
   
ya checked, that also working fine..
JL_Coder 10-Jan-14 5:15am
   
please check update.
Karthik_Mahalingam 10-Jan-14 6:00am
   
ok run the code, in the browser see the source code, and try to find the text editor, and see the ID,, use that id as selector..
hi JL_Coder,

try this

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function () {
           $('#answ').hide();
           $("#btnsubmit").click(function () {
               var tbans = $('#txtMain').val();
               alert(tbans);
               $('#answ').toggle();
               return false;
           });
       });
   </script>


<textarea id="txtMain" runat="server" rows="20" cols="150"></textarea>
   <input type="submit" id="btnsubmit" />
   <label for="male" id="answ">
   </label>
   
Comments
JL_Coder 10-Jan-14 4:31am
   
it shows blank alert after i tried writing ('#txtMain').
Bojjaiah 10-Jan-14 8:36am
   
it's working for me,

can u refer this <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
Use
var tbans = $('#<%= txtMain.ClientID %>').val();


instead of

var tbans = $('<%= txtMain.ClientID %>').val();


# is missed.
   
Comments
JL_Coder 10-Jan-14 4:32am
   
it shows blank alert after i tried writing in your way.
You have to replace your next code line (that has error!):
var tbans = $('<%= txtMain.ClientID %>').val();


with the next one one:

var tbans = $('#txtMain').val();
   
Comments
JL_Coder 10-Jan-14 4:32am
   
it shows blank alert after i tried writing in your way.
Raul Iloc 10-Jan-14 4:42am
   
Try then to replace function val() with text().

var tbans = $('#txtMain').text();
JL_Coder 10-Jan-14 5:15am
   
please check update.
As I was using Tinmce editor, I had use below code to get the content of the textarea. It worked for me.
var tbans = window.parent.tinymce.get('txtMain').getContent();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900