Home>

With jquery, I want to use the value of the function processing result (or variable defined in the function) in the change or click event in another process.
How should I set the return value?

* Reason: To handle the variable with ajax communication.

// Substitute the value of input and textarea updated in the browser to the variable
  $('# id_url'). change (function () {
    var inputVal_url = $(this) .val ();
    console.log (inputVal_url);
  });
    // assign the value of the radio button selected on the browser to the variable
$(". page_name_btn"). click (function () {
    var radioVal_page_name = $("input [name = 'page_name']: checked"). val ();
    console.log (radioVal_page_name);
  • Answer # 1

      

    Function processing results in change and click events

    I don't understand the meaning
    If the data is passed during "ajax communication", you can refer to the value at that time

    $('# id_url'). val ()

    $("input [name = 'page_name']: checked"). val ()

    I think there will be no changes or clicks

  • Answer # 2

    I think yambejp's answer is enough, but
    Ifyou don't want to or can't get it during ajax communication processing, can you assign it to a global scope variable?

    // Variable declaration in scope outside both events
    let inputVal_url,
      radioVal_page_name;
    // Substitute the value of input and textarea updated in the browser to the variable
    $('# id_url'). change (function () {
      // assign to global variable
      inputVal_url = $(this) .val ();
      console.log (inputVal_url);
    });
    // assign the value of the radio button selected on the browser to the variable
    $(". page_name_btn"). click (function () {
      // assign to global variable
      radioVal_page_name = $("input [name = 'page_name']: checked"). val ();
      console.log (radioVal_page_name);
      // Other processing below?
    });

  • Answer # 3

    Thank you all.
    Also, sorry for the lack of background. m () m

    Because it is an article editing screen, when the page is loaded, the value is reflected in the value of input and radio button.
    Since it was necessary to get the value for the user to enter the change contents in the input field, it was obtained by change or click.

    Making use of global scope variables in miyabi_takatsuk's content
    I was able to get the value to send safely with ajax.
    Thank you very much.

    let inputVal_url,
        inputVal_wire_url,
        inputVal_project,
        textareaVal_coment,
        radioVal_page_name,
        radioVal_job,
        radioVal_style;
        // Substitute the value of input and textarea updated in the browser to the variable
      $('# id_url'). change (function () {
        inputVal_url = $(this) .val ();
        console.log (inputVal_url);
      });
       // input and radio description continues ...