I have 2 div,in both the div i have the same id but different class. When clicked on one div we can get its attribute id.its ok.. This id is same for the other div. How can i get the class of this 2nd div using this id?
<div class="oneclass" id="same">Hi this is the first div</div>
<div class="secondclass" id="same">Hi This is the second div</div>
<script type="text/javascript">
$("div.secondclass").click(function () {
pos=$(this).attr('id');
alert("the id of the 2nd div is ="+pos);
});
</script>
You cannot have two elements with the same id like that. That is an invalid HTML document and any DOM inspection/manipulation relying on duplicated id attributes is likely going to have unpredictable results across different browsers.
That said, if I had to quickly and roughly edit your above code to do what you’re asking, I’d end up with something like this:
There may be a more succinct and/or more typically jQuery way to do it than the jQuery above, but the above works and is close enough to your initial code that you should be able to follow the changes very easily.