Reputation: 786
I'd like to set a variable to equal the text of a substring. Specifically, if a[href] begins with "download.php", I'd like to set the variable "elm" to the value of a[href], after the 19th character.
if ("a[href^='download.php']") {
var elm = $('a[href]'.substring(19));
};
I've tried using .text() and .html(), but can't get it working. Can someone point out my errors? Many thanks.
Upvotes: 1
Views: 931
Reputation: 2740
You dont need your if statement, since you are passing a string which is a truthy value.
To get the value of href
atributte use $.attr
var elm = $("a[href^='download.php']").attr('href').substring(19);
and if you want to check if exists any element who match with [href^='download.php']
use
var el = $("a[href^='download.php']");
if(el.length) {
var elm = el.first().attr('href').substring(19);
}
Upvotes: 2
Reputation: 41440
var a = $("a[href^='download.php']");
if (a.length) {
var elm = a.attr("href").substring(19);
}
Upvotes: 2