Jquery in ASP.net

Hi there

I am using Jquery 1.3.2 with VS 2008 Professional (>net 3.5)

Just wondering how I can pass an element id to Jquery within a Javascript function

I have included a simple example to illustrate this.

See the html code for the button :
 <button id="MyButton" onclick="MyClick(this.id)">Click me for jquery</button>

It calls the onlclick event and I pass the element id to MyClick()

The function MyClick(x) knows what x is - in this case it is MyButton

However, when the use $(x).html("Hello Again") - it does not work

Any help will be appreciated



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="Ajax_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script  language="javascript" type="text/javascript">
        function MyClick(x) {
            alert('The element id is ' + x);
            $(x).html("Hello again");       //this does not work 
            $("#MyDiv").html("Hello Again");
    <form id="form1" runat="server">
        <button id="MyButton" onclick="MyClick(this.id)">Click me for jquery</button>
        <br />
        <div id="MyDiv">
               This is a div

Open in new window

Who is Participating?
eyal_mtConnect With a Mentor Commented:
I assume it is now working because you have changed the code to one of the above solutions, because the original code does not work.
If so, please award points.
Anurag ThakurTechnical ManagerCommented:
statcomAuthor Commented:
Sorry, couldn't see anything in the post which will help me

I have tried to use a straight javascript function and it works :

document.getElementByID(x).innerHTML = "Hello Again";

However if I use the jquery counterpart it does not work :

$(x).html("Hello Again");

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

You are forgetting that the selector by ID for jQuery is #ID.
So your code should be:

$("#"+x).html("Hello Again");
Actually, depending on what you actually need to do in your function, you may consider changing the even on your button to send the actual DOM element and not the ID like so:


and then you can leave your selector as is:
$(x).html("Hello Again");
statcomAuthor Commented:
Ooops. it works fine now , thank you
I have added new jquery version onthe top of the script.problem is resolved with this

    <script src="js/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="js/jquery-1.4.1.js" type="text/javascript"></script>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.