javascript - Change value and link depending on radio button selection - Stack Overflow

I'm trying to get the text value and the link to change depending on which radio button is selecte

I'm trying to get the text value and the link to change depending on which radio button is selected. For example, selecting the first radio button would change the text to "Google" and it would link to google. While selecting the second radio button would change the text to "bing" and link to bing. I have managed to get the text to change, however the link is not working correctly. Any help would be appreciated.

jsfiddle - /

$('.radiogroup').change(function(e){
    var selectedValue = $(this).val();
    $('#amount').val(selectedValue)
});
<script src=".3.1/jquery.min.js"></script>
<input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" />
    <input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" />

   
  <input type="submit" name="amount" id="amount" onclick='myFunction()' />
  <script>
    function myFunction() {
   
        if(document.getElementById('radiogroup1').checked) {
            document.getElementById('amount').href = "";
        }
    }
    
</script>

I'm trying to get the text value and the link to change depending on which radio button is selected. For example, selecting the first radio button would change the text to "Google" and it would link to google.. While selecting the second radio button would change the text to "bing" and link to bing.. I have managed to get the text to change, however the link is not working correctly. Any help would be appreciated.

jsfiddle - https://jsfiddle/3yrcusv8/

$('.radiogroup').change(function(e){
    var selectedValue = $(this).val();
    $('#amount').val(selectedValue)
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" />
    <input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" />

   
  <input type="submit" name="amount" id="amount" onclick='myFunction()' />
  <script>
    function myFunction() {
   
        if(document.getElementById('radiogroup1').checked) {
            document.getElementById('amount').href = "https://google.";
        }
    }
    
</script>

Share Improve this question asked Dec 30, 2018 at 18:46 user7548188user7548188 2
  • You should have a separate a element for your link – Jack Bashford Commented Dec 30, 2018 at 18:51
  • Submit should be used with a form, you submit a form, not simply submit an input, but you can also simply redirect the user inside the function, also changing the href of the submit input doesn't make sense, it doesn't take a href attribute. – Art3mix Commented Dec 30, 2018 at 18:53
Add a ment  | 

4 Answers 4

Reset to default 3

Explanation

Okay, so rather than having a large if statement, or a large switch statement, you can set a HTML attribute to contain the relevant URL, in my answer, you can see that I've used data-url. Personally I find this cleaner for the JavaScript side of things, it also means less logic to be processed within the JavaScript run time too.

Considering with the change function, you reference the radio button that has been clicked, you can just access the data-url attribute from there, and just update the a tag.

Another note, using an a tag over a button/input tag makes more sense as an a tags purpose is pretty much to be a link on a page, I'd imagine using your original approach may cause some bugs, especially in specific browsers, at least with an a tag, you know it'll work.

$('.radiogroup').change(function(e) {
  const $this = $(this), $link = $("#url");
  $link.html($this.val());
  $link.attr("href", $this.attr("data-url"));
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" data-url="https://google." />
<input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" data-url="https://www.bing./" />

<a id="url" href="" target="_blank">null</a>

Edit

This solution simply uses native JavaScript, nothing else.

const a = document.getElementById("url");
const inputs = document.querySelectorAll('.radiogroup');

// A simple function to handle the click event for each input.
const clickHandler = i => {
  a.href = i.getAttribute("data-url");
  a.textContent = i.getAttribute("value");
};

// Possibly even less code again. 
inputs.forEach(i => i.onclick = () => clickHandler(i));
<input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" data-url="https://google." />
<input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" data-url="https://www.bing./" />

<a id="url" href="" target="_blank">null</a>

Not sure why you try to change the href of the submit input, the input doesn't take href as an attribute and it won't do anything if it was changed as well.

Simply redirect the user when they click the button inside the function.

function myFunction() {
    if(document.getElementById('radiogroup1').checked) {
        window.location.replace("http://google.");
    } else {
      window.location.replace("http://bing.");
    }
}

You can use custom attributes to pass the URL around as below, if you want the user to click the button to open the page.

Google refuses the connection from their end, but you can see from the Bing option that the code works.

// Add click event to radiogroup
$('.radiogroup').change(function(e) {

  // Update url attribute and value of button from radio button
  $('#amount').attr("url", $(this).attr("url")).val($(this).val());
 
});

// Add click event to submit button
$(document).on("click", "#amount", function() {

  // Print URL to console (Can be deleted)
  console.log( $(this).attr("url") );
  
  // Open page in window
  window.location.assign($(this).attr("url"));

});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" url="http://google."/> Google
<input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" url="http://bing." /> Bing (working)


<input type="submit" name="amount" id="amount"  />

We can use a dynamic function to pull this off.

You can simply make a dynamic function in JS that will work for n number of radial buttons by fetching their value attribute and id and change the input button accordingly.

The code snippet:

<input id="google" type="radio" name="radiogroup" class="radiogroup" value="Google" onclick="changeLink(event)" />
<input id="bing" type="radio" name="radiogroup" class="radiogroup" value="Bing" onclick="changeLink(event)" />
<input type="submit" name="amount" id="btn" />
    <script>
      const changeLink = e => {
          let _target = e.currentTarget.id, //get the id name.
              targetValue = document.getElementById(_target).getAttribute('value'), //use id name to fetch value attribute.
              btn = document.getElementById('btn'); //get the btn element from DOM.

        btn.setAttribute('value', targetValue); //set btn elements 'src' attribute to the value of the radial button clicked.
        btn.setAttribute('src', 'https://' + targetValue.toLowerCase() + '.'); //same src but convert to lower case first.
        
        console.log(btn); //for testing the link
      }
    </script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744757088a4591947.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信