Set the input tag type and name with jQuery's attr() method to avoid any string escaping issue

Factorise some code too