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 #
Related
I am trying to find first row first anchor tag in jquery ?
I have 4 rows .I want to get first row anchor tag. I tried like this
https://jsbin.com/woxatuxoju/edit?html,js,output
$(()=>{
console.log( $('a[title=" Nominal"]')[0])
$('a[title=" Nominal"]')[0].css({'background-color':'red'})
})
but not able to find correct element.
$('a[title=" Nominal"]')[0] returns the DOM object of element and not the jquery object. Due to this .css () method is not applying the changes.
You should use .eq() selector to get jquery object of element by its index:
$('a[title=" Nominal"]:eq(0)').css({'background-color':'red'});
Using Javascript:
document.querySelectorAll('a')[0].style.backgroundColor = 'red';
Working Demo
I have a span with a text inside it like :
<span class="characters-count">(160 Characters Left)</span>
given the span a class in this case .characters-count , when i tried class selector using jQuery to get the text inside span :
$(".characters-count")[0].text();
it returns undefined !
However this selector works well :
$("span.characters-count").text();
anyone can explain what is happening ?
$("span.characters-count").text();
In our case you work with jQuery Object that has text method
$(".characters-count")[0].text();
In this case you work with actual DOM element (like document.getElementByClassName('characters-count')[0]) that does not have text method
Use
$(".characters-count").text();
Demo
console.log($(".characters-count:first").text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="characters-count">(160 Characters Left)</span>
try something like this:
$(".characters-count:first").text()
Check here, why it was not working for you.
//This will return dom element, so it doesn't have `text()` method, that's why it was throwing error for you.
console.log($(".characters-count:first")[0]);
// This will return object of an element, you can use jQuery selectors to get first element from collection and set text to it
console.log($("span.characters-count"));
console.log($("span.characters-count:first").text
);
you need to use innerText instead of text() when you use $(".characters-count")[0] check DEMO
$(".characters-count")[0].innerText
I need to add an additional limiter to this find() method so that the <a> elements whose text begin with a string of Re: are left out.
$(document).find(".middletext:not(.quoteheader) > a[href^='http://fakeURL.com/']")
The problem is that I don't know of an attribute to represent the text within an <a> tag, and I don't know of a selector that only selects strings that don't begin with something. If a text attribute existed and if a selector that selects strings that don't begin with something looked like this: ^!=, then my code would be:
$(document).find(".middletext:not(.quoteheader) > a[href^='http://fakeURL.com/'][text^!='Re: ']")
How can I make this work?
I suspect there is a way to use filter() after using find() and make it work, but I don't know how.
I'd suggest, as you say, using filter():
$(document).find(".middletext:not(.quoteheader) > a[href^='http://fakeURL.com/']")
.filter(function(){
return $(this).text().indexOf('Re:') !== 0;
}).css('color','red'); // or whatever
References:
JavaScript:
String.prototype.indexOf().
jQuery:
filter().
find().
text().
I have a variable named MYCELL which is a reference to a td inside a table.
the td looks as follows
<td>
abc
edit
</td>
Right now i wish to replace that string abc with def but keep the link.
I thought i could use the following code:
MYCELL.not(".myLinkClass").empty();
MYCELL.append("def");
But the empty() clears the entire cell.
Based on this link i opted to use a selector inside my .not() statement
MYCELL.not(MYCELL.find(".myLinkClass")).empty();
Any idea on how to empty (including other html-tags) everything in my td without removing any links (<a href>) or any elements with the class .myLinkClass ?
Thank you for your time
You need to use contents() to ensure you can select text nodes as well:
MYCELL.contents().filter(function() {
return !$(this).is("a"); // Only select nodes where are not <a> tags
}).remove(); // Use .remove instead
jsFiddle
I'm trying to do something similar to this question, but it's a bit different, so the solution there isn't working for me.
<span class="a-class another-class test-top-left"></span>
I have an element (this code shows a span but it could be div span or anything). This element has a class beginning with test- (test-top-left, test-top-right etc.) I've triggered a click event on classes starting with test- and saved the clicked object as var object = this;. Simple stuff so far.
What I'm trying to do now is get the full name of that class (test-top-left). I know it starts with test- but what's the full name. The thing is that there are other classes a-class another-class and test-top-left. Can hasClass be used to get the full name of the class? I'd prefer not to use find() or filter() just because there may be additional elements within that also have class="test-"
Edit:
The code I have now is, but it gives me ALL the classes. What I need is the single class beginning with test-.
var object = this;
$(object).attr('class');
So now I for loop through all the classes and test each one separately, which seems like a lot of unnecessary code. I'm hoping jQuery has a clever way to get the exact class that was clicked right away.
Description
You can use jQuerys Attribute Contains Selector, .attr() and .click() method.
Attribute Contains Selector - Selects elements that have the specified attribute with a value containing the a given substring.
.attr() - Get the value of an attribute for the first element in the set of matched elements.
.click() - Bind an event handler to the "click" JavaScript event, or trigger that event on an element.
Sample
html
<span class="anyclass test-hello">Hello World</span>
jQuery
$("[class*='test']").click(function() {
var object = $(this);
alert(object.attr("class").match(/(test-.*?)(?:\s+|$)/)[1])
;});
Check out the updated jsFiddle
Update
If you dont want to use regex you can do this.
$("[class*='test']").click(function() {
var object = $(this);
alert("test-" + object.attr("class").split("test-")[1].split("-"))
;});
More Information
jQuery - Attribute Contains Selector
jQuery - .attr()
jQuery - .click()
jsFiddle Demonstration
This should work for you:
var object = this;
var className = object.className.match(/(test-.*?)(?:\s+|$)/)[1];
Class name is the name of the class you are looking for.
If you don't want to use split or regex, you can try having the class in a separate attribute
<span class="someclass test-something" _rel="test-something">test<span>
or
<span class="someclass" _rel="test-something">test<span>
with the script
$("[_rel*='test-']").click(....
And to retrieve the attribute, use $(this).attr("_rel")