tags:

views:

88

answers:

5

When a button is clicked it needs to look before its ocurance and find a specific span tag. Unfortunately it is either not finding the tag or not changing it's contents.

This is the code that doesn't work.

$(this).prev("span[id*=CommentText]").hide();

I can set the colour during the load event but when the link is clicked it won't make any changes.

This works but only during the load phase:

$("span[id*=CommentText]").css("background-Color", "red");

EDIT:

<html>
<head>
    <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 src="../Scripts/jquery.color.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("textarea[id*=Comments]").hide().css("background-Color", "white");
            $("span[id*=CommentText]").css("background-Color", "red");
            $("a[id*=CommentLink]").click(LoadComments).css("background-Color", "white");
        });
        function LoadComments() {
            $(this).prev("textarea[id*=Comments]").animate({ backgroundColor: 'yellow' }, 500).show().focus();
            $(this).prev("span[id*=CommentText]").css("background-Color", "white");
            $(this).fadeOut();
        }
    </script>
</head>
<body>
     <span id="Q37CommentText">comments here</span>
     <textarea name="Q37Comments" rows="1" cols="50" id="Q37Comments">comments here</textarea>
     <a id="Q37CommentLink">Add/Edit Comment</a>

     <span id="Q40CommentText">Comment 2</span>
     <textarea name="Q40Comments" rows="1" cols="50" id="Q40Comments">Comment 2</textarea>
     <a id="Q40CommentLink">Add/Edit Comment</a>
</body>
</html>
A: 

try this $("span[id*='CommentText']")

antpaw
Sorry but this doesn't work.
Middletone
A: 

Have you tried using the .live event to bind the click event? Binding to the click event will only work with elements that haven't been dynamically added.

http://docs.jquery.com/Events/live

Mike Robinson
The click event does fire as the other elements on the page change correctly. Using the first line $("span[id*=CommentText]").css("background-Color", "red");I can change the properties during he click event but I can't seem to get at the individual element.
Middletone
+1  A: 

The problem with .prev() is that according to the docs:

Only the immediately previous sibling is returned, not all previous siblings.

You'd probably have to use prevAll("span[id*='CommentText']") (not tested).

http://docs.jquery.com/Traversing/prevAll

Januz
Good answer; the bottom line is "that's not how prev(expr) works".
Stobor
+1  A: 

Try this:

$(this).parent().find("span[id*=CommentText]").hide();

edit: you may or may not need the child selector >. Since I use ASP.NET, I have to use it quite often.
edit: yeah, you don't need the child selector, so I removed it.

Jim Schubert
Good answer. This works in my case where the elements are in a sort of container but it won't work if they ar ein the same container.
Middletone
I see what you mean. My comment referred to your original post where the elements were within TD containers. As your post is currently, it wouldn't work. I would suggest using certain naming conventions and selecting based on id instead of DOM position. After all, you'll have to revisit this issue if you decide to split comments or add an element between the anchor and the span.
Jim Schubert
+4  A: 

If the structure of your markup is always the same, I'de be inclined to just keep it simple and go with

$(this).prev().prev() ...

Other alternatives are using prevAll()

$(this).prevAll('span:first'); // may or may not need the attribute filter depending
                            // on if there are more <span> elements as siblings

Navigating up to the parent and then calling find()

$(this).parent().find('span[id*=CommentText]');

A side note:

Just looking at your markup, it may be easier to use CSS classes as opposed to ids and attribute filters to select elements based on ids beginning with, ending with or containing x. It's likely to be faster in all/nearly all browsers (particularly those that implement document.getElementsByClassName(classNames) or the Selectors API)

Russ Cam
$(this).prev().prev().css("background-Color", "white").hide(); works and won't fail in the case that the elements are in the same container.
Middletone
+1 great answer!
Jim Schubert