0

I want to How to add two functions value to a single input. It means I have a dropdown list and an input field. I want to add dropdown list value and input field value to another input field. But according to my code, it overrides value. I can not keep both dropdown list value and an input field value. I want to know how can I keep both dropdown list value and an input field value in the second input field.

function changeFunc() {
    $('#currency_name').val($('#cur_rate :selected').text());
}
function click_key() {
    $('#currency_name').val($('#cost_price').val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<select id="cur_rate" onchange="changeFunc();">
    <option>a</option>
    <option>b</option> 
    <option>c</option> 
    <option>d</option> 
</select>
<input type="text" id="cost_price" placeholder="Enter Cost Price" name="cost_price" onkeyup="click_key();">

<input id="currency_name" name="currency_name">

2
  • Make a third function that returns the sum of the returned values from the other two functions and have the element's handler call the third function. Commented Jul 15, 2020 at 13:46
  • I would introduce a button that triggers a function that combines the values of both inputs and adds them as value of the currency_name input. Commented Jul 15, 2020 at 14:16

1 Answer 1

1

You can create other function and add the values or just do conditions in those functions you already have done as I indicate in the snipet

function changeFunc() {
  $('#currency_name').val('');
  if ($('#cost_price').val() !== '') {
    $('#currency_name').val($('#cur_rate :selected').text() + $('#cost_price').val());
  } else {
    $('#currency_name').val($('#cur_rate :selected').text())
  }
}

function click_key() {
  $('#currency_name').val('');
  if ($('#cur_rate :selected').text() !== '') {
    $('#currency_name').val($('#cur_rate :selected').text() + $('#cost_price').val());
  } else {
    $('#currency_name').val($('#cost_price').val())
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<select id="cur_rate" onchange="changeFunc();">
  <option>a</option>
  <option>b</option>
  <option>c</option>
  <option>d</option>
</select>
<input type="text" id="cost_price" placeholder="Enter Cost Price" name="cost_price" onkeyup="click_key();">

<input id="currency_name" name="currency_name">

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your effort. But according to your code, it iterates the input field value. Then expected output not shows.
@user_v12 I think that I miss understood what you were asking. Check now the snipet :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.