2

I have created a button on my page that its HTML looks like this:

enter image description here

Now in my JavaScript I have a jQuery that finds that button like this:

 $('.personlistbtn').click(function(e) {
    console.log("inside click event of person list");
    console.log("provider id was this " + provider_id);
    // ????? 
  });

I want to attach some parameters to my href button that posted above so it can go to a new page in my Rails app with those parameters I am passing to it so hopefullu something like this:

<a href="/pharmacy/patients?provider_id=234" >

Notice I attached that ?provider_id=234 to it. How can I do that?

Sergio
  • 28,539
  • 11
  • 85
  • 132

3 Answers3

5

Assuming provider_id is already set elsewhere in your code, this should work:

$('.personlistbtn a').click(function(e) {
    e.preventDefault();
    window.location.assign(this.href + '?provider_id=' + provider_id);
});     
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • my href at first was /pharmacy/patients now it shows as /pharmacy/undefined –  Aug 09 '13 at 13:51
  • @EricFromSouthPark this code does not interfere with the HTML code at all, it just redirects on click based on the values it has access to. – Rory McCrossan Aug 09 '13 at 13:52
  • @EricFromSouthPark, do you get a value from `provider_id` in your console? maybe that is what is not working? – Sergio Aug 09 '13 at 13:54
  • @Sergio : yep, provider_id has a value. –  Aug 09 '13 at 13:55
1

You have to edit the href attr:

 $('.personlistbtn').on('click', function() {
     var $a = $(this).find('a');
     var href = $a.attr('href');
     $a.attr('href', href + '?provider_id=234');
 });

EDIT:

That code will work when clicking on the container div. If you want to attach the parameters when clicking on the anchor, you should do the following:

 $('.personlistbtn').on('click', 'a', function(e) {
     e.preventDefault();
     window.location = $(this).attr('href') + '?provider_id=234';
 });
viarnes
  • 2,018
  • 2
  • 19
  • 31
  • 3
    `$(this)` inside that click function has no href. `$(this)` refers to the div. Add `a` after class and a `e.preventDefault();` – Sergio Aug 09 '13 at 13:46
  • 1
    Note that the page will already have performed the redirect to the original `href` value before you get to set the new value. – Rory McCrossan Aug 09 '13 at 13:48
  • @RoryMcCrossan : Then this approach won't work for me. I want to first set those parameters and then make the call so the Rails controller can work on those params and show the right content on the page. –  Aug 09 '13 at 13:50
  • Sorry, you should add a .find('a') to the $(this) – viarnes Aug 09 '13 at 13:52
  • @Servani : Can you please update the code to the more correct thing you think it should be ? Thanks. –  Aug 09 '13 at 13:54
0

Try this:

$('.personlistbtn a').click(function (e) {
    e.preventDefault();
    var provider_id = '?provider_id=234'; // this you already have, I just put it to have a value
    var newurl = this.href + '?provider_id=' + provider_id
    window.location.assign(newurl);
});

JSFIDDLE

Rikard
  • 7,485
  • 11
  • 55
  • 92