0

I'm trying to execute a jQuery script to change the data-url attribute based on which radio button is clicked/selected.

But my code won't work when I put it inside the if statement to check which radio button is active. It will successfully replace the data-url attribute though when executing without an if statement.

$('input[name="radio1"]').on('change', function() {
  if ($(this).val() == 'yearly') {
    var a = $('#samcart').data('url');
    $('#samcart').attr("data-url", "www.urlhere.com");
  } else if ($(this).val() == 'quarterly') {
    var a = $('#samcart').data('url');
    $('#samcart').attr("data-url", "www.url2here.com");
  } else {
    var a = $('#samcart').data('url');
    $('#samcart').attr("data-url", "www.url3here.com");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="samcart" class='samcart-popup-container' style='margin:auto;width:200px;align:center;' data-id='123' data-url='empty'>
  <div class='samcart-popup-button' type='button' class='btn btn-default css3button'>
    Join
  </div>

12
  • Hi Nardi, and welcome. Can you provide the code that comprises the <input name="radio1" ...>?
    – Forty3
    Aug 23, 2017 at 22:22
  • Hi Forty3, thank you! <input type="radio" name="radio1" id="yearly" value="yearly"><label class="yearly-label four col" for="yearly">I have some text here</p></label> And two other radio buttons with yearly and quarterly values instead of monthly. Aug 23, 2017 at 22:45
  • Where are the radio buttons in your HTML?
    – Barmar
    Aug 23, 2017 at 23:21
  • You shouldn't mix .attr() and .data() to access data attributes, maybe that's your problem. See stackoverflow.com/questions/28335833/…
    – Barmar
    Aug 23, 2017 at 23:22
  • @Barmar I kinda feared that, but how can I make it work since $('#samcart').attr("data-url", "www.url3here.com"); was the only method that actually changed the data-url value Aug 23, 2017 at 23:34

1 Answer 1

0

EDITED It's seem your code is good, I do not substantial changes, see the jsfille below:

https://jsfiddle.net/7tL6dfft/16/

HTML

<form action='#' method='get'>
<input type="radio" name="radio1" id="yearly" value="yearly"><label class="yearly-label four col" for="yearly">I have some text here</label>
<input type="radio" name="radio1" id="quarterly" value="quarterly"><label class="yearly-label four col" for="yearly">quarterly</label>
<input type="radio" name="radio1" id="weekly" value="weekly"><label class="yearly-label four col" for="yearly">weekly</label>

</form>
<a id="samcart" href='#' data-url='http://exmaple.com'>sam cart url</a>

JS

$(function(){
console.log("start");

$('input[type=radio][name=radio1]').on('change', function() {
  console.log("event",$(this).val());
  if ($(this).val() == 'yearly') {

    $('#samcart').data("url", "www.urlhere.com");
    $('#samcart').html("www.urlhere.com");
    var a = $('#samcart').data('url');
  } else if ($(this).val() == 'quarterly') {

    $('#samcart').data("url", "www.url2here.com");
    $('#samcart').html("www.url2here.com");
    var a = $('#samcart').data('url');
  } else {

    $('#samcart').data("url", "www.url3here.com");
    $('#samcart').html("www.url3here.com");
    var a = $('#samcart').data('url');
  }
  alert("new data-url: "+a);
});
})

May be you were mislead by

  • your debug, you take the "var a" before change the value
  • your browser, sometimes the change of attrs is not show by developer tool
  • the position of the js, you set your JS before the code in the loading chain, see $(function(){})
1
  • Thanks but It's still not working. To clarify, I have used multiple methods for adding the if statements and I had no ideas left. None of them worked Aug 23, 2017 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.