1Line
1Line

Reputation: 87

jquery getting and passing href to a function

Im having some problems getting the href of the clicked element and passing it to a function, this is something i have not done before so done my best but have not got it. Any ideas?

$('#smoke_confirm').click(function(e){
            var href = $(this).attr("href");
            tstconfirm(href);
            e.preventDefault();
        });

        function tstconfirm(href){
            smoke.confirm('Are you sure you want to delete?',function(e){
                if (e){
                    window.location = $(href);
                }
            }, {cancel:"cancel", ok:"confirm"});
        }

Upvotes: 0

Views: 1250

Answers (2)

PSL
PSL

Reputation: 123739

href here is a text so $(href) is not correct as it will try to select element with the href value. Just do window.location = href. Also if you just want to get the href you dont need to create a jquery instance of this you could just do this.href which is a DOM element property.

$('#smoke_confirm').click(function(e){
            var href = this.href;
            tstconfirm(href);
            e.preventDefault();
        });

        function tstconfirm(href){
            smoke.confirm('Are you sure you want to delete?',function(e){
                if (e){
                    window.location = href;
                }
            }, {cancel:"cancel", ok:"confirm"});
        }

Upvotes: 2

Sushanth --
Sushanth --

Reputation: 55750

href is a string

 window.location = $(href); // This will try to convert it to a jQuery object

supposed to be

 window.location = href;

Upvotes: 1

Related Questions