Search code examples
jqueryhrefwindow.location

find a tag that contains the window.location.href


In order to replace the text that displays the language on my page, I want to find the 'a' tag that contains the window.location.href url and set its text as the current language text

I've tried doing: a[href=window.location.href] but it doesn't work

var href = window.location.href;
var currentLangVal = jQuery('.countrySelector a[href=href ]').text();
jQuery('.language-display').text(currentLangVal);

Solution

  • What you need is String Concatenation, in this case, as you want the value of the href variable, and not "href" itself.

    var href = window.location.href;
    var currentLangVal = jQuery('.countrySelector a[href="' + href + '"]').text();
    jQuery('.language-display').text(currentLangVal);