JavaScript Change value of textbox based on radio box selection change value textbox jquery,change value textbox javascript,change value

I am trying to change the value of a textbox based on the selection a group of 4 radio buttons. This is not the actual html code, but a simplified version:

    <input type="radio" name="radiogroup" id="radiogroup" value="5" />
    <input type="radio" name="radiogroup" id="radiogroup" value="10" />
    <input type="radio" name="radiogroup" id="radiogroup" value="15" />
    <input type="radio" name="radiogroup" id="radiogroup" value="20" />

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

So what I am trying to do is fill the textbox named "amount" with either 5, 10, 15 or 20 based on which radio button is selected.

I am new to jquery and everything I tried did not work.

Thank you in advance for any help.

cdr6545

Answer:1

You can easily do this by adding class.

Working Fiddle

HTML:

    <input type="radio" name="radiogroup" id="radiogroup" class="radiogroup" value="5" />
    <input type="radio" name="radiogroup" id="radiogroup" class="radiogroup" value="10" />
    <input type="radio" name="radiogroup" id="radiogroup" class="radiogroup" value="15" />
    <input type="radio" name="radiogroup" id="radiogroup" class="radiogroup" value="20" />

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

JS:

$('.radiogroup').change(function(e){
    var selectedValue = $(this).val();
    $('#amount').val(selectedValue)
});
Answer:2

ID's are unique, and an ID can only be used on one element in the current document, so change it to classes.

Then attach a change event handler to the radios, get the checked one, and set the value of the text input to the checked radios value

$('.radiogroup').on('change', function() {
  $('#amount').val( this.value );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" name="radiogroup" class="radiogroup" value="5" />
<input type="radio" name="radiogroup" class="radiogroup" value="10" />
<input type="radio" name="radiogroup" class="radiogroup" value="15" />
<input type="radio" name="radiogroup" class="radiogroup" value="20" />
<br /><br />
<input type="text" name="amount" id="amount" />
Answer:3

Say I have the following construct appearing frequently in my code, if (e.which==13){ ... } ...and I would like to make the code look a bit cleaner by writing instead: ifEnterPressed{ ... } Is ...

Say I have the following construct appearing frequently in my code, if (e.which==13){ ... } ...and I would like to make the code look a bit cleaner by writing instead: ifEnterPressed{ ... } Is ...

I have a function like this inside a NodeJs app function dojob(url){ var resultList = []; request (url, function (error, response, html){ if(!error){ var $ = cheerio.load(html); $(...

I have a function like this inside a NodeJs app function dojob(url){ var resultList = []; request (url, function (error, response, html){ if(!error){ var $ = cheerio.load(html); $(...

  1. nodejs cheerio function

I am trying to convert two strings to same format like toUpperCase/toLowerCase to compare two strings regardless of case sensitive in javaScript. Below is my function. function submitForm() { ...

I am trying to convert two strings to same format like toUpperCase/toLowerCase to compare two strings regardless of case sensitive in javaScript. Below is my function. function submitForm() { ...

  1. uncaught typeerror cannot read property
  2. uncaught typeerror cannot read property 'addeventlistener' of null
  3. uncaught typeerror cannot read property 'length' of undefined
  4. uncaught typeerror cannot read property 'style' of null
  5. uncaught typeerror cannot read property 'fn' of undefined
  6. uncaught typeerror cannot read property 'type' of undefined
  7. uncaught typeerror cannot read property 'value' of null
  8. uncaught typeerror cannot read property 'appendchild' of null
  9. uncaught typeerror cannot read property '0' of undefined
  10. uncaught typeerror cannot read property 'style' of undefined
  11. uncaught typeerror cannot read property 'mdata' of undefined
  12. uncaught typeerror cannot read property 'setstate' of undefined
  13. uncaught typeerror cannot read property 'call' of undefined
  14. uncaught typeerror cannot read property 'top' of undefined
  15. uncaught typeerror cannot read property 'replace' of undefined
  16. uncaught typeerror cannot read property 'map' of undefined
  17. uncaught typeerror cannot read property of null
  18. uncaught typeerror cannot call a class as a function

i have this site: link There is a difference when you call function in document.ready calculation or resize function There is a white border when the page loads calculation is not correct ... $( ...

i have this site: link There is a difference when you call function in document.ready calculation or resize function There is a white border when the page loads calculation is not correct ... $( ...