Ciprian
Ciprian

Reputation: 3226

Get element that doesn't have a specific id - jQuery

This is pretty simple ... but I just can't get it to work. This is what I m doing.

<li><a href="#" class="myClass" id="idOne">Click1</a></li>
<li><a href="#" class="myClass" id="idTwo">Click2</a></li>

If I click Click1 pickID will be idOne, but I don't know how to get the idTwo. What s the syntax for getting the id from the element with class myClass that doesn't have the id pickID ?

$(".myClass").click(function(e) {
    e.preventDefault;

    var pickID = $(this).attr('id');
    var notPickId = $('myClass').attr(not(pickID));// ??? this is probably so wrong ... I didn't even try it 

});

Upvotes: 0

Views: 1171

Answers (3)

Try

var notPickId = $('.myClass').not(this).attr('id');

or

var notPickID = $('.myclass').not('#' + pickID).attr('id');

or

var notPickId = $(this).parent().siblings().find('a').attr('id');

this keyword

.not()

Upvotes: 1

user3401335
user3401335

Reputation: 2405

another way

var pickID = $(this).attr('id');
$(".myClass[id!='" + pickID  + "']").attr("id")

Upvotes: 0

Michiel
Michiel

Reputation: 2203

Try this:

var notPickID = $(".myclass:not(#" + pickID + ")");

Upvotes: 0

Related Questions