i need to select the value of second to last input selectable element:
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
The select input tag are inside tr tags.
If I use $("select.x").last(), jQuery select the last element. I need to select second to last.
You can use .eq() with negative indexes:
$("select.x").eq(-2);
These negative indexes are "1-indexed": -1 gives the last element, -2 the penultimate, and so on.
You can use .prev()
$("select.x").last().prev();
All of the below will do the trick (select the second last element):
$("select.x").eq(select.length - 1)
$("select.x:nth-last-of-type(2)")
$("select.x:nth-last-child(2)")
$("select.x").last().prev()
You can use :last selector and move to the preceding element using prev:
$("select.x:last").prev();
Ref:
Get the immediately preceding sibling of each element in the set of
matched elements, optionally filtered by a selector.
Sample demo: http://jsfiddle.net/IrvinDominin/ck8XP/
You need to use "nth-last-child(2)" of jquery, this selects the second last element.
You can check this here:
https://api.jquery.com/nth-last-child-selector/
The solutions with .prev() or nth-last-child() don't works.
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
The problem is the last().prev() functions return the the object <a> which i suppouse come first the select one.
The nth-last-of-type(2) selector instead return an empty object.
Related
I need to be able to insert some markup into a document after() the second P element in a container. If there is only one element then I need to insert after() that one. nth-child simply does not make a match if there is only one element. How can I do this?
Thanks!
Select them both, and grab the first match.
$("#foo > p:nth-child(2), #foo > p:lastChild").eq(0).append(...;
Because the results are returned in document order, you can use the 0 index to get the nth-child or if it wasn't there, it'll get the last child.
If there are other element types, and you only care about the p elements, then use nth-of-type instead.
$("#foo > p:nth-of-type(2), #foo > p:lastChild").eq(0).append(...;
I am new to Jquery and I've had a look through the replies on here but I can't find any specific answers to my question.
I have a (ASP) generated table a snippet of which is:
<a href="javascript:__doPostBack('gv2','Select$15')"
style="color:White;">S</a></td><td style="font-size:XX-Small;">1104</td>
<td style="font-size:XX-Small;">320.20.0116.090</td>
<td style="font-size:XX-Small;">*Not Found*</td>
What I'm trying to do is highlight the *Not Found text and then disable the preceeding href so the link cannot be clicked.
I have developed the following selector:-
$('td').highlight('Not Found').each(function(){$(this).prev("a").removeAttr("href")});
The highlight selector works but the removeattr doesn't. Syntax is probably incorrect but any pointers would be very useful.
Answered:- This works
$("td:contains('*Not Found*')").each(function(){$(this).parent().find('a').removeAttr("href")})
I'd personally suggest:
// selects all 'td' elements
$('td').filter(function(){
// retains only those whose text is precisely '*Not Found*'
return $.trim($(this).text()) == '*Not Found*';
// moves to the closest (ancestor) 'tr'
// finds the 'a' element within that 'tr' element
// sets the 'href' attribute to be equal to '#'
}).closest('tr').find('a').attr('href','#');
JS Fiddle demo.
Alternatively, rather than setting the href to #, you could just remove the a element by unwrapping its contents:
$('td').filter(function(){
return $.trim($(this).text()) == '*Not Found*';
}).closest('tr').find('a').contents().unwrap();
JS Fiddle demo.
References:
attr().
closest().
contents().
filter().
find().
jQuery.trim().
text().
unwrap().
try $(this).prev().find("a").removeAttr("href")}
also removing the link might not work.
try replacing the href with #
I have some div tag below:
<div class="magazine"></div>
<div class="newsletter"></div> // I need to take this div
<div class="may-moon"></div>
If I needed div with class start with "ma", I would use $('div[class^="ma"]'), but what is opposite? thanks.
The opposite would be to use jQuery :not():
$('div:not([class^="ma"])')
You can use the negative filtering function "not" like this: $('div').not('[class^="ma"]'), or the negative selector ":not" like this: $('div:not([class^="ma"])') (as pointed by Karl-André Gagnon)
You need to use :not() Selector for this. because there is no exact opposite selector exist of [^=] and * in jquery.
:not() Selector - Selects all elements that do not match the given selector.
See more about Jquery selectors
There a opposite selector exist ! -
Attribute Not Equal Selector [name!="value"] - Select elements that either don’t have the specified attribute, or do have the specified attribute but not with a certain value.
but use of this ! selector, you need to provide full-name of class.
$('div[class!="magazine"][class!="may-moon"]')
Try This
I have the following html
<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>
How would I find the div elements which don't have a class attribute? ie three and four?
You can use :not selector
$('div:not([class])');
here is API
And a simple Fiddle
Use :not selector to filter
$('div:not([class])');
Combine the :not() selector with the attribute present selector [class].
$("div:not([class])")
jsFiddle.
Another option is to use .not() with Has Attribute Selector
$('div').not('[class]')
There are different ways to do it. You could use .children() to get the list and then index into that. Or you could look up the second element and use .next() to get its sibling.
Assuming you're not wanting to select all the dividers which have no classes, you can use nth-child to select specific ones if you know exactly where they are within a container. This will select your class-less dividers:
$('div:nth-child(3)') // Selects the third divider
$('div:nth-child(4)') // Selects the fourth divider
$('div:nth-child(3), div:nth-child(4)') // Selects both
JSFiddle example.
Alternatively you can select using .prev() and .next():
$('div.two').next() // Selects the divider after div class="two"
$('div.three').prev() // Selects the divider before div class="three"
Is there a way to only use ONE dollar sign instead of doing this?
$($(".elem")[0]).hide()
I could use :first, but what about if I wanted the third or so:
$($(".elem")[2]).hide()
Use .eq() function
$(".elem").eq(3).hide()
Description: Reduce the set of matched elements to the one at the specified index.
.eq( index )
indexAn integer indicating the 0-based position of the element.
And
.eq( -index )
-indexAn integer indicating the position of the element, counting backwards from the last element in the set.
And it is 0 index based, so third would be .eq(2)
You can use, :eq
$('.test:eq(2)').hide(); //hides the third encounter of an element with class .test
There is also :nth-child( x ) but it grabs a child element.
Read more,
http://api.jquery.com/eq-selector/
http://api.jquery.com/nth-child-selector/