Home > Software design >  how to get input group radio value from input text box
how to get input group radio value from input text box

Time:09-25

I need to store the input fields with selected ones, how can I be able to do that with help of jQuery, Javascript, any idea?

<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
    <input type="radio" name="choosen">
    </div>
  </div>
 <input type="text"  name="opt[]"  />
  <br />
</div>

<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
    <input type="radio" name="choosen">
    </div>
  </div>
   <input type="text"  name="opt[]"  />
  <br />
</div>

CodePudding user response:

It depends on a few things:

Where do you want to store?

If you intend to store it in the browser, then you can use localStorage or sessionStorage to do so. If you want to store it on the server, then you will need to send it somehow, maybe as form data, or an AJAX request, or via WebSocket.

When do you want to invoke the store?

If you want to invoke the store upon each value change, then you could add a change event, like:

let options = document.querySelectorAll("input[name=choosen]");
for (let option of options) {
    option.addEventListener('change', function() {
        storeCheckedOption(options);
    });
}

So far, so good. Now let's implement storeCheckedOption:

function storeCheckedOption(options) {
    for (let option of options) {
        if (option.checked) {
            store("choosen", option.value);
            return;
        }
    }
    store("choosen", "");
}

Now let's implement store. For now I assume that you intend to store it into localStorage, but you can change the way things are stored in the way you intend:

function store(key, value) {
    localStorage.[(value ? "set" : "remove")   "Item"](key, value);
}

Naturally, you can do this with AJAX as well if you intend to store it on server-side.

However, if you want to store the chosen radio button on the server when the form is being submitted, then you do not need to do anything on the client-side. Instead, you can parse the request parameters on server-side and store the chosen item there.

CodePudding user response:

Here is the code :)

var store;

$('button').on('click', function(){
    let value = $('input[type=radio]:checked').parents('.input-group').find('input[type=text]').val();
  store = value;
  
  console.log(store);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
    <input type="radio" name="choosen">
    </div>
  </div>
 <input type="text"  name="opt[]"  />
  <br />
</div>

<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
    <input type="radio" name="choosen">
    </div>
  </div>
   <input type="text"  name="opt[]"  />
  <br />
</div>
<br>
<button>Store</button>

CodePudding user response:

You can get the input field value, by clicking on the radio button. Make sure you add something on the input field, before click on the radio button. Checkout the Fiddle

$('input[type=radio]').on('click', function() {

  var inputValue = $('input[type=radio]:checked').closest('.input-group').find('input[type=text]').val(); /* You can use let instead of var */

  console.log(inputValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
      <input type="radio" name="choosen">
    </div>
  </div>
  <input type="text" name="opt[]" />
  <br />
</div>

<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
      <input type="radio" name="choosen">
    </div>
  </div>
  <input type="text" name="opt[]" />
  <br />
</div>

  • Related