[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

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

Cheers

AL

<%@ 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">
    <title>hello</title>
    <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");
        };
 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <button id="MyButton" onclick="MyClick(this.id)">Click me for jquery</button>
        <br />
        <div id="MyDiv">
               This is a div
        </div>
    </div>
    </form>
</body>
</html>

Open in new window

0
statcom
Asked:
statcom
1 Solution
 
Anurag ThakurCommented:
0
 
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");

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

$("#"+x).html("Hello Again");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eyal_mtCommented:
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:

onclick="MyClick(this)"

and then you can leave your selector as is:
$(x).html("Hello Again");
0
 
statcomAuthor Commented:
Ooops. it works fine now , thank you
0
 
eyal_mtCommented:
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.
0
 
srinukasCommented:
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>
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now