Search code examples
jquerytypesattributesready

jQuery input attribute change from x to y


I try to replace the type of a input through jquery from "date_calendar" to "date". The input's name is "DateOfBirth[0]", but for some reason it doesn't work with the code bellow..

I am new to jquery/javascript and maybe I am missing something.. How can I do it? What did I missed? This is the current code:

$(document).ready(function() {
$('input[name=DateOfBirth[0]]').attr('type', 'date');
});

HTML:

<input min="1938-04-28" max="1995-04-28" class="form_input_date" type="date_calendar" name="DateOfBirth[0]">

Thank you very much..

Edit: I tried the double backslash [name=DateOfBirth\\[0\\]] and to quote the name value [name="DateOfBirth[0]"] but it still doesn't work..


Solution

  • Simply do this:

    $('input[name="DateOfBirth[0]"]').attr('type', 'date');
    
    • You need to double quotes the name here.
    • Without quotes, you will get error like:
      • Syntax error, unrecognized expression: input[name=DateOfBirth[0]]

    FIDDLE