-1

I have 2 radio buttons, the second is with input field. I need to get the value of the selected, and in case of the second, value from the input field. The thing is, that when the second button is selected, its input value is empty until user types something there. As a result, I have always empty field.

The markup and code is below

<input id="x" type="radio" name="re" value="Undefined" checked>No limit
<input id="y" type="radio" name="re" value="limit">Limit 
<input id="y-input-radio" type="number" min="1" max="20" placeholder="Up to 20"/>
    //tweet limit
    $('input[type="radio"]').on('click', function(){
     var value = $(this).val();
     if(value === 'limit'){
        var tweetLimit = document.getElementById('y-input-radio').value;
        console.log(tweetLimit); //returns 0 cos its still empty
     }
    });

I cheked setTimeout but I am not sure how to implement it and maybe there is another solution

Anna
  • 914
  • 9
  • 25
  • 1
    Try getting the value of the input with .change(). See: https://api.jquery.com/change/ – digitalsuite.net Nov 23 '20 at 10:43
  • Duplicate of [How can I know which radio button is selected via jQuery?](https://stackoverflow.com/questions/596351/how-can-i-know-which-radio-button-is-selected-via-jquery) – Heretic Monkey Nov 23 '20 at 13:55

2 Answers2

1

You can use $("input").change()

So every time there is a change in value it will be taken

Examle:

//tweet limit
$('input[type="radio"]').on('click', function () {
    var value = $(this).val();
    if (value === 'limit') {
        $("input").change(function () {
            var tweetLimit = $('#y-input-radio').val();
            console.log(tweetLimit);
        });
    } else {
        $("input").off('change');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<input id="x" type="radio" name="re" value="Undefined" checked>No limit
<input id="y" type="radio" name="re" value="limit">Limit
<input id="y-input-radio" type="number" min="1" max="20" placeholder="Up to 20" />
54ka
  • 3,501
  • 2
  • 9
  • 24
0

Could you use the change() function on your input ? IT would trigger an event after a change has been made in the text input.