Caio Ferrari
Caio Ferrari

Reputation: 319

Get the href value using jQuery

I got this Codrops tutorial for customize a drop-down list (the example one, more specifically). I had no deal for this: just copied and pasted the mark up and jQuery and customized my own style.

However, if you check the example one, there is a jQuery at the very end where gives us a JavaScript snippet to make the button display the selected value. Ok, cool! However, I notice something! It made my link stopped working. Since I'm going to use some PHP values and # in the <a> tags inside, whenever you click in the link, the placeholder works, but the link doesn't.

The code below is for the drop down effect

function DropDown(el) {
  this.dd = el;
  this.initEvents();
}
DropDown.prototype = {
  initEvents : function() {
    var obj = this;

    obj.dd.on('click', function(event) {
      $(this).toggleClass('active');
      event.stopPropagation();
    });
  }
}

And this one for the placeholder:

function DropDown(el) {
    this.dd = el;
    this.placeholder = this.dd.children('span');
    this.opts = this.dd.find('ul.dropdown > li a');
    this.val = '';
    this.index = -1;
    this.initEvents();
}
DropDown.prototype = {
    initEvents : function() {
      var obj = this;

      obj.dd.on('click', function(event){
        $(this).toggleClass('active');
        return false;
      });

      obj.opts.on('click',function(){
        var opt = $(this);
        obj.val = opt.text();
        obj.index = opt.index();
        obj.placeholder.text(obj.val);
      });
    },
    getValue : function() {
        return this.val;
    },
    getIndex : function() {
        return this.index;
    }
}

Thank you soooo much!

Upvotes: 0

Views: 989

Answers (1)

user3263978
user3263978

Reputation: 193

var href = $(this).attr('href');

set a variable for the href to use, if you want to target a specific a tag then replace 'this' with its class, use attr to select an attribute and href is the attribute you want to get, this will store the href value in a variable called href

Upvotes: 1

Related Questions