views:

35

answers:

1

I have a tree view control build dynamically. i want to change color of the selected node .one help me to write the script given below. and its work fine.

 <script type="text/javascript"  src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">
    google.load("jquery", "1.4.2");
    google.setOnLoadCallback(function() {
        //change cursor to hand when user mouseover tree nodes
        $(".TreeView1_0").mouseover(function() {
            $(this).css('cursor', 'pointer');
        });


        //unbold all nodes then bold the selected node to indicate it's selected
        $(".TreeView1_0").click(function() {
            $(".TreeView1_0").css('font-weight', 'normal');
            $(".TreeView1_0").css('color', 'black');
            $(".TreeView1_0").css('background-color', 'white');


            $(this).css('color', 'white');

            $(this).css("background-color", "blue");

        });
    });
</script>

Now i want to change the source file to the js file stored in script folder.And store selected node index and value in hidden fields.i have a source file JQuery1.4.1.js in script folder. i dont know what is the exact way of referencing to this js file. and i dont know how to retrieve selected node index and value

i changed the code to do that.the entire aspx code is shown below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server"> <title></title>

   <script type="text/javascript" src="../../Scripts/JQuery1.4.1.js"></script>
  <script type="text/javascript">


        //change cursor to hand when user mouseover tree nodes
        $(".TreeView1_0").mouseover(function() {
            $(this).css('cursor', 'pointer');
        });


        //unbold all nodes ,then bold the selected node to indicate it's selected ,and store  selected node index and value to two hidden fields
        $(".TreeView1_0").click(function() {
            $(".TreeView1_0").css('font-weight', 'normal');
            $(".TreeView1_0").css('color', 'black');
            $(".TreeView1_0").css('background-color', 'white');
            $(this).css('color', 'white');
            $(this).css("background-color", "blue");

          // i am not sure about the two lines of code given below
            document.getElementById('hfvalue').value = $(this).value;
            document.getElementById('hfindex').value =$(this).index;

            $(this).css('color', 'white');

            $(this).css("background-color", "blue");

        });

</script>

</head>

<body>

<form id="form1" runat="server">
<div>
    <asp:TreeView ID="TreeView1" runat="server">
    </asp:TreeView>
</div>
<p>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:HiddenField ID="hfvalue" runat="server" />
    <asp:HiddenField ID="hfindex" runat="server" />
</p>
</form>

</body> </html>

But the code is not working . i am a newbie. Any suggestions

A: 

I guess, first you need to make a small change in your script. Check out the following snippet to use document.ready -

$(document).ready(function(){

   //change cursor to hand when user mouseover tree nodes

   $(".TreeView1_0").mouseover(function() {

       $(this).css('cursor', 'pointer');

   });

   //unbold all nodes ,then bold the selected node to indicate it's selected ,and store  selected node index and value to two hidden fields

   $(".TreeView1_0").click(function() {

       $(".TreeView1_0").css('font-weight', 'normal');

       $(".TreeView1_0").css('color', 'black');

       $(".TreeView1_0").css('background-color', 'white');

       $(this).css('color', 'white');

       $(this).css("background-color", "blue");

     // i am not sure about the two lines of code given below

       document.getElementById('hfvalue').value = $(this).value;

       document.getElementById('hfindex').value =$(this).index;

       $(this).css('color', 'white');

       $(this).css("background-color", "blue");

   });

});

Also, please make sure that the jquery script does get picked up properly, and the path you are using is appropriate. A simple way to verify this is to type the following into the address bar and see if the alert shows up a function

javascript:alert($)

Kartik
i tried by adding $(document).ready(function() .but its giving an error -->Microsoft JScript runtime error: Object expected
Shameer
That pretty much means your script path is not proper. Did you verify using javascript:alert($), as suggested above?
Kartik
yeah . thanks. its working now. any idea about how i can assign the selected node value and index to the hidden fields ? document.getElementById('hfvalue').value = $(this).value; document.getElementById('hfindex').value =$(this).index; is it a correct way ? .thanks
Shameer
Hey, I don't understand your question, but anyways, if my answer solved your problem, just wanted to remind you that you might want to accept it :)
Kartik
haha. ok. didnt get the exact answer. anyway i am happy :) .thank u
Shameer