views:

38

answers:

2

how do i unset this option from a input?

im using ajax to populate this field and after that i need to show it

thanks

+2  A: 

You can't reliably alter the type of an input, but you can create a new element and copy the various attributes across:

$(document).ready(
 function(){
  $('<input type="text" />')
     .appendTo('form')
     .val($('input:hidden[name=nameOfHiddenElement]').val())
     .attr('name','nameAsAppropriate')
     .remove('input:hidden[name=nameOfHiddenElement]');
 }
);

Link to a (basic) JS Fiddle demo

David Thomas
A: 

I would recommend against creating an <input type="hidden" />, then trying to awkwardly transform it into the type you're after. Instead, create it with whatever type you need (text, select, etc.), and use CSS to hide it initially.

In your view, for example:

<style type="text/css">
input.hidden { display: none; }
</style>
<?php echo $this->Form->input('Model.fieldName', array('type'=>'text','class'=>'hidden'))?>

Then, in your AJAX callback, reveal the input:

$.ajax('/ajax/url/here', {}, function(response, status){
    // perform your field population, then...
    $('input.hidden').show();
});
Daniel Wright
is there an easiest way to do this in cakephp?
juan