1

cannot access asp.net server side control from jquery

Label

<label id="lblClanName" runat="server">Something here</label>

Jquery

  $('#test').click(function () {
        var yle = ('#"<%= lblClanName.ClientID %>"').val;
        alert(yle);
    });

button:

<input type="button" id="test" value="TesTme" />

alert returns undefined

5 Answers 5

3
 var yle = ('#"<%= lblClanName.ClientID %>"').val;

Should be:

 var yle = $('#<%= lblClanName.ClientID %>').val();
Sign up to request clarification or add additional context in comments.

1 Comment

shouldn't be $ be placed before ('#<%= lblClanName.ClientID %>').val(); or im missing something
1

You should not have double quotes and also you are missing $ and () in val method call in your code. Try this.

$('#test').click(function () {
    var yle = $('#<%= lblClanName.ClientID %>').val();
    //Note here
              ^                                     ^
              |                                     |
             $ was missing                      () as missing
    alert(yle);
});

Update

Since you are trying the get the text of a label element, use text() method instead of val(). Try this.

var yle = $('#<%= lblClanName.ClientID %>').text();

4 Comments

@Acid - Because label do not have value use text() instead it will work.
:) i already tried Text(), innercontent, innertext, innerhtml some shit here
When i used jquery in asp.net always was problem. Does it only my problem?
Use text() and not Text(). JavaScript is case sensitive.
0

You need parenthesis for your val function call:

//notice the `$` to make this a valid jQuery selection, 
//also the double quotes have been removed from the selector,
//and the parenthesis were added to the `val()` call
var yle = $('#<%= lblClanName.ClientID %>').val();

You were referencing the val function but not running it.

You would reference a function as you have if you were trying to just pass a reference to the function without running it, for example:

setTimeout(myFunc, 5000);

Will call the function named myFunc after five seconds.

1 Comment

Good catch, you should explain why you made the changes to the code that you did...
0

Just add () to the val function and remove the ":

  $('#test').click(function () {
        var yle = $("#<%= lblClanName.ClientID %>").val();
        alert(yle);
    });

Comments

0

Your line..

var yle = ('#"<%= lblClanName.ClientID %>"').val;

Shoud be..

var yle = $('#"<%= lblClanName.ClientID %>"').val();

The two things where missed in your line... jQuery selector function which is.. '$' and .val should be .val()

Hope this helps!

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.