18

I have a few asp:textbox controls in a form on a webpage, below is a snippet. The first is a field where the recipient is entered, the other is a larger textarea where the recipients name should be loaded into, along with some other text.

<asp:TextBox name="recipient" ID="recipient" class="inputBox" onChange="addNames()" runat="server" />
<asp:TextBox TextMode="MultiLine" name="usermessage" ID="usermessage" class="usermessage" height="128" width="425px" runat="server"></asp:TextBox>

A standard message is loaded into this second textbox with use of JQuery with this code:

$(".usermessage").val("Hello etc");

This works nicely, the message is shown.
When a user enters the name of a recipient or his own name in one of the other textboxes, addNames() is triggered. This function adds the name of the recipient to the standard message in the usermessage box.

function addNames() {
    //update textbox
    var recipient = $(".recipient").val();
    var sender = $(".name").val();
    $(".usermessage").val("Hello " + recipient +", \nThis is a message. \n\rKind regards, \n" + sender);
    }

Problem is that the two variables recipient and sender are "undefined".

Hello undefined,
This is a message.

Kind regards,
undefined




Actual question: What is the correct code to retrieve the value from an asp:textbox if this

var recipient = $(".recipient").val();  

does not work?

The output in the html is as follows:

<input name="ctl00$contentPlaceHolderRightColumn$recipient" type="text" id="ctl00_contentPlaceHolderRightColumn_recipient" name="recipient" class="inputBox" onChange="addNames()" />

I'm using JQuery v1.3.2, with Firefox v3.5.3.

Kablam
  • 2,494
  • 5
  • 26
  • 47

6 Answers6

25

As far as I can tell doing

var recipient = $(".recipient")

Will select all dom elements with a CLASS of recipient. Your input box has a class of "inputBox".

You need to select by its ID using the #

So:

var recipient = $("#recipient")

But you are using ASP.NET controls, which give it a unique ID generated on the server so it's unique. (in your case it's ctl00_contentPlaceHolderRightColumn_recipient)

To select you will need to do

var recipient = $("#<%=recipient.ClientID%>")

-edited out some syntax errors

Chris James
  • 11,571
  • 11
  • 61
  • 89
  • This put me on the right track. Both your solution: var recipient = $("#<%=recipient.ClientID%>").val(); AND var recipient = $(".recipient").val(); [with the class 'recipient' added to the textbox control], worked. :) Thanks! – Kablam Oct 23 '09 at 10:54
  • Arghh. I need to read jQuery doc myself, how could I miss that "." in the selector )-. Good spot. – Audrius Oct 23 '09 at 10:54
3

If you set the clientID of the asp textbox, you should be able to use that in JQuery in the same way as you have, but witha hash instead of the dot.

E.g.

ClientID = "recipient"

then

    var recipient = $('#recipient').innerHTML() 

or

    var recipient = $('#recipient').text() 
Sabyasachi Mishra
  • 1,677
  • 2
  • 31
  • 49
Jimmeh
  • 2,834
  • 1
  • 22
  • 21
  • Sadly, to no avail. I still get the "undefined"'s as output. – Kablam Oct 23 '09 at 10:10
  • Ofcourse: – Kablam Oct 23 '09 at 10:18
  • 1
    Did you try with the ClientID set and $('#recipient'), btw? It was an edit. If that doesn't work, i'm afraid i'm out of ideas. – Jimmeh Oct 23 '09 at 10:22
  • 1
    Also, it may be worth using .innerHTML rather than .val, as I'm not sure the value of the textbox will be updated. – Jimmeh Oct 23 '09 at 10:24
  • Yes, I did. :) And that makes two of us, for the being out of ideas part. – Kablam Oct 23 '09 at 10:25
  • That last solution would have been perfect... but no results. As with #recipient nor without the preceding dot. – Kablam Oct 23 '09 at 10:27
  • have you tried adjusting it so that it just uses document.getElementById('recipient') with clientID set, to see whether it's just through JQuery that it doesn't work? – Jimmeh Oct 23 '09 at 10:30
  • Yes, I tried that as well. Thanks so much for all your efforts! <3 Qui's answer did the trick. – Kablam Oct 23 '09 at 10:56
2

Through this you can also get asp button or any control for that matter.

var txt_recipient = document.querySelector("input[id*='recipient']");
alert(txt_recipient.Value);

you can also try querySelectorAll() if you have multiple controls with same id in client side.

jQuery Equalent is

$("input[id*='recipient']").val()
George
  • 2,842
  • 2
  • 15
  • 11
1

I think that jQuery selector returns you a list of objects. Have you tried:

var recipient = $(".recipient")[0].val();

UPDATE

What about this then:

var recipient = $(".recipient:first").val();

It has a nice advantage as well - will brake on the first match.

Audrius
  • 2,836
  • 1
  • 26
  • 35
  • This is a genious idea, but sadly it does not work. FireBug says: "$(".recipient")[0] is undefined". – Kablam Oct 23 '09 at 10:06
  • No, still no luck :( It's a selector problem though, that much is clear. If I enter $('.bogus').val();, with or without preceding dot, I get the same result. "Hello undefined,". – Kablam Oct 23 '09 at 10:18
  • 1
    Can you check and paste the produced HTML that you see in a browser (source)? Check if name property is not changed to the usual "ctrl00_" ASP.NET way. What jQuery version and browser are you using? A few times I've encountered a issue (bug?) with jQuery 1.2x when I couldn't use control id as a selector and had to resort to this "$('[id="control_id"]')". Try $('[name="recipient"]') or $('[name^="recipient"]') if name is changed by ASP.NET. – Audrius Oct 23 '09 at 10:29
  • Yes, the produced output is altered to the "ctl00" etc. But your $('[name="recipient"]') does not change anything... I'm lost. – Kablam Oct 23 '09 at 10:46
  • Ok then, your altered control name should still end with "recipient", try 'name$="recipient"' selector. It selects all controls that have "name" attribute ending with "recipient". Please refer to docs.jquery.com/Selectors for all available selectors. – Audrius Oct 23 '09 at 10:53
-1

i guess the asp.net HTML controls must have ClientID="recipient" property to access the control using client id in client side. or i have no idea!

<asp:TextBox ID="recipient" ClientID="recipient" runat="server" name="recipient" class="inputBox" onChange="addNames()" />

try it this should work.

Hardy
  • 1
  • 5
-3

IN ASP.NET, you will need to write in this way:

var recipient = $("#<%=recipient.ClientID%>").val();

In MVC, then you can write in this way:

var recipient = $("#recipient").val();
Mnemo
  • 1
  • 2