Changing the type of an input field using jQuery (text -> password)

前端 未结 5 2233
天涯浪人
天涯浪人 2020-12-15 10:17

I am trying to change type of input, specifically after click into input change type=\"text\" to change=\"password\".

I don\'t know why, but I can\'

5条回答
  •  半阙折子戏
    2020-12-15 10:54

    Your code throws an error for me, type property can't be changed. Interestingly though, the following code does work for me on Chrome at least:

    $('input#id_reg_pass')[0].type = 'password';
    

    It could be that this doesn't work cross-browser however, which would explain why jQuery doesn't allow it (As pointed out by Niklas, this is the case). Alternatively you could construct a new with the same attributes and replace the old one with it.

提交回复
热议问题