Timmy Von Heiss Timmy Von Heiss - 3 months ago 10
jQuery Question

What do I use to target this input to disable it with jquery .attr("disabled", "disabled")

i have tested the code and it works on other inputs but for some reason this input won't work. The input uses the rails-jquery-autocomplete gem which uses jquery autocomplete

rails code:

<%= f.autocomplete_field :tag_list, autocomplete_tag_name_photos_path, :"data-delimiter" => ', ', 'data-auto-focus' => true, :id_element => '#tag_element' %>


source code:

<input data-delimiter=", " data-auto-focus="true" data-autocomplete="/photos/autocomplete_tag_name" data-id-element="#tag_element" type="text" name="photo[tag_list]" id="photo_tag_list" class="ui-autocomplete-input" autocomplete="off">


script

$(document).ready(function(){
$('.glyphicon-plus').on("click", function(){
if($(window).width() > 768) {
console.log
$('???').attr("disabled", "disabled")
}
});
});


I have tried: #photo_tag_list, #tag_element, #photo.tag_list.ui-autocomplete-input, input#photo_tag_list.

Answer

TRy following code.

$(document).ready(function(){
  $('.glyphicon-plus').on("click", function(){
    if($(window).width() > 768) {

      $('#photo_tag_list').attr('readonly', true);// IF JQUERY 1.9+   $('#inputId').prop('readonly', true);
    }
  });
});

Reason: Input type text doesn't suport disabled, to make the input type text disabled we need to use readonly attribute.