Jquery in

Posted on 2009-04-26
Last Modified: 2012-05-06
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(">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" "">

<html xmlns="">

<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(">Click me for jquery</button>

        <br />

        <div id="MyDiv">

               This is a div






Open in new window

Question by:statcom
    LVL 26

    Expert Comment

    by:Anurag Thakur

    Author Comment

    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");

    LVL 10

    Expert Comment

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

    $("#"+x).html("Hello Again");
    LVL 10

    Expert Comment

    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");

    Author Comment

    Ooops. it works fine now , thank you
    LVL 10

    Accepted Solution

    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.

    Expert Comment

    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>

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now