0

I want change hidden input value from other input value.

This is code:

 $(document).ready(function() {
$(\'input:text[name="'.$data['name'].'"]\').keyup(function() {
 $(\'input:hidden[id="'.$data['name'].'"]\').val( "'.$data['id'].'," + $(this).val() );
});
 });

This code work but i don't know how to change from keyup to always set value even if he does not edit the field and the field contains data from the database If I don't edit the field, it won't load anything into the hidden field

I entered various functions in various ways and nothing

14
  • Can you clarify what you mean by to always set value even if he does not edit the field; why would it need to be updated if the original is not edited? What does your html look like? You should be able to initialize the value of the hidden input the same way you do the text input. Commented Nov 29, 2023 at 19:00
  • because i must have value in input. I delete all data from database when click send and add new edited. This is only one way for my project for edit data by admin. Commented Nov 29, 2023 at 19:02
  • @mykaf I have in database same data id with other values so edit this data is impossible. Data must have same id in my project... Commented Nov 29, 2023 at 19:05
  • What about using the change event instead of keyup? Commented Nov 29, 2023 at 19:24
  • @mykaf if i set to change() work only when change input value Commented Nov 29, 2023 at 19:27

1 Answer 1

0

try to use following code , it changes the value of input:text when input:hidden value changes.

$(document).ready(function() {
        // this part handles when input:text field changes value 
            $('input:text[name="' + $data['name'] + '"]').on('input', function() {
                $('input:hidden[id="' + $data['name'] + '"]').val($data['id'] + ',' + $(this).val());
              });

// this part handles when loading page , set value of hidden input by value of input:text value 
    let $textInput = $('input:text[name="' + $data['name'] + '"]');
        let $hiddenInput = $('input:hidden[id="' + $data['name'] + '"]');
   let $textval = $textInput.val();
      $hiddenInput.val($textval);
            });
Sign up to request clarification or add additional context in comments.

2 Comments

Work only when i set input value
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.