ARTLoe ARTLoe - 3 months ago 8
jQuery Question

converting from Javascript to JQuery - Rails 4

Could one kindly advise me how i would write the below javascript in jQuery? i am unsure as my converted code is not working


application.js


document.getElementById("userj_cvattachment").onchange = function () {
document.getElementById("uploadFile").value = this.value;
};

Answer

Lets break it down.

  1. You select an element by: document.getElementById("userj_cvattachment")

    $("#userj_cvattachment")
    
  2. You attach a handler to its change event: .onchange = function (){ }

    $("#userj_cvattachment").on("change", function(){ });
    
  3. You select another element by id

  4. You change its value: .value = this.value;

    $("#userj_cvattachment").on("change", function(){ 
        $("#uploadFile").val($(this).val());
    });