4

I have a select menu with a link to redirect the customer once he/she selects the option that he/she wants. My question would be: how I can start with the link disabled with javascript to force the customer to select one of the options? Below I paste the code that I have:

<select name="dept" class="select_type" id="PT_Certificate_options">
    <option value="#">Select Study Option</option>
    <option value="mydomain.com/option1">Distance</option>
    <option value="mydomain.com/option2">Part Time</option>
    <option value="mydomain.com/option3">Full Time</option>
</select>
<a id="certificate_enrol_link" href="#" class="orange_btn">ENROL NOW</a>

<script type="text/javascript">
    $("#PT_Certificate_options").change(function () {
        console.log(this.value);
        $("#certificate_enrol_link").attr('href', this.value);
    });
</script>

Thank you for your time.

rrk
  • 14,861
  • 4
  • 25
  • 41
Albert
  • 157
  • 2
  • 14

5 Answers5

7

Just start with no href attribute:

<a id="certificate_enrol_link" class="orange_btn">ENROL NOW</a>
ED-209
  • 4,558
  • 2
  • 19
  • 25
4

First of all, you would have to use full URLs when appending the href to these a's, because otherwise your URL's will not work. Second, since I noticed your ENROL NOW a has a class="orange_btn", I'm assuming you want it to be a submit button instead of a text link. Disabling a button is a lot easier than disabling a text link, so I changed it into a button for you.

Here is the final working example (provided you have jQuery loaded, which you have, I presume):

$("#PT_Certificate_options").change(function() {
  console.log(this.value);
  if (this.value != "#") {
    $("#certificate_enrol_link").prop('disabled', false)
  } else {
    $("#certificate_enrol_link").prop('disabled', true)
  }
});

$("#certificate_enrol_link").click(function() {
  window.location.href = $('#PT_Certificate_options').val();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="dept" class="select_type" id="PT_Certificate_options">
  <option value="#">Select Study Option</option>
  <option value="http://example.com/option1">Distance</option>
  <option value="http://example.com/option2">Part Time</option>
  <option value="http://example.com/option3">Full Time</option>
</select>
<button id="certificate_enrol_link" href="#" class="orange_btn" disabled>ENROL NOW</button>

Edit: as Musa kindly pointed out, buttons cannot have href's, so I had to make a slight alteration to make the button actually redirect to the selected link, but the above example works now :)

Laurens Swart
  • 1,124
  • 7
  • 20
  • 1
    buttons don't have hrefs – Musa Jan 11 '16 at 17:01
  • Thank you Musa, I was just about to try to do the 'a's in grey myself but I didn't figure out how yet :) – Laurens Swart Jan 11 '16 at 17:01
  • I have updated the example to circumvent the "buttons don't have hrefs" problem. Thank you for pointing this out. It's a fully working example now. Keep in mind the redirect to example.com might not work in this snippet preview, but if you copy-paste it to your website, it will work! – Laurens Swart Jan 11 '16 at 17:08
  • 1
    You should really be using a button for this sort of thing +1 – Musa Jan 11 '16 at 17:10
  • 1
    perfect +1 you can use short IF $("#certificate_enrol_link").prop('disabled',(this.value != "#")?0:1) ; prop vs attr jQuery ver http://stackoverflow.com/questions/1414365/disable-enable-an-input-with-jquery – Valentin Petkov Jan 11 '16 at 18:12
0

Start without the href attribute or, if you want to remove it programmatically use:

$("#certificate_enrol_link").removeAttr("href")
Kurt Van den Branden
  • 9,082
  • 8
  • 60
  • 72
0

Another approach could be:

$('#certificate_enrol_link').click(function(event) {
  if ('#' == $(this).attr('href')) {
    alert("Please select an option!");
    return false;
  }
});
Alex
  • 9,339
  • 2
  • 28
  • 46
-1

try this fiddle

i have used preventDefault() to disable the link

$('.link_dis').on('click',function(e){
        e.preventDefault();
})

  <a id="certificate_enrol_link" class="link_dis"href="#" class="orange_btn">ENROL NOW</a
Siddharth
  • 838
  • 8
  • 16