tags:

views:

47

answers:

4

Hi,

I have a structure something similar to,

<div class="div1">
    <a class="myimg" href="#">clickme1</a>
    <span>myspan1</span>
    </div>

    <div class="div2">
     <a class="myimg" href="#">clickme1</a>
      <span>myspan2</span>
    </div>

CSS

<style type="text/css">
    .div1
    {
      border: 1px solid red;    

    }
     .div2
    {
      border: 1px solid black;    

    }
     .test
    {
      border: 1px solid yellow;    

    }

    </style>



<script type="text/javascript">
        $(function () {
            $("a.myimg").click(function () {
                alert($(this).html());
                $("span").addClass("test");

            });



        });

now when I click on <a> span in both the divs are getting affected, What I want is if I click on div1 <a> then it should affect span1, and same for div 2. How to achive his in jquery.

    </script>

EDIT: Is it possible by using context, something similar to,

$("a.myimg",context).click(function () {
A: 
$(this).next("span")

inside your event handler will return the immediately following span element.

Matti Virkkunen
+2  A: 

Change your code to specify the span you want to modify (The one next to the image) with .next("span")...

$(function () {
    $("a.myimg").click(function () {
        alert($(this).html());
        $(this).next("span").addClass("test");
    });
});
Quintin Robinson
thnx for the ans, I have edited my post, can u pls take look at it.
Wondering
@Wondering Are you worried the elements will not be adjacent? Given the nesting of the elements the answer might require some tweaking, what is your exact problem/goal? Why do you need to provide context? What would the context be and where would it come from?
Quintin Robinson
ok, I mean to say, can I pass the <div> element as a context, i.e. if I click on 1st <a> then it should affect span of first div, and same should happen in case of 2nd div
Wondering
@Wondering okay no problem, I misread the nesting anyway due to the indentation. You can find the parent `div` from the image, if you are concerned about it and then find the `span` from that div, although the `next` solution seems more concise if your elements will be adjacent. I will update my answer to address your other question when I get a moment, I became busy with work suddenly.
Quintin Robinson
ok, np, pls update when u have some time.
Wondering
@Wondering after further review I have come to the conclusion that passing the context has no real value in the problem specified. Is there some reason you would like to pass context rather then inferring from the anchor?
Quintin Robinson
I just wanted to know, how to write this using context, no specific reason as such
Wondering
A: 
$(function () {
    $("a").click(function () {
        $(this).next("span").html("Span Changed")
    });
});​

http://jsfiddle.net/dBPb8/

NAVEED
A: 

use sibblings method

$(function () {
            $("a.myimg").click(function () {  
                alert($(this).html());
                $(this).siblings('span').addClass("test");
            });
        });
Praveen Prasad