Passing JS created values to hidden form elements


i searched but was unable to find a direct solution.

i have a need to embed values into hidden form elements, after they are generated within a javascript call on the form’s page.

the javascript is google maps returning lat/long and i need to pass that to hidden form elements on the same page.

right now, the javascript is in the form partial. is it necessary to wrap that within a yii convention in order to access the generated hidden elements?

i’ve tried directly accessing the hidden form elements via the DOM/js and setting them that way, but it doesn’t seem to be working.

any pointers to the function or examples are greatly appreciated.


If the hidden form fields are generated from a model with CHtml::activeHiddenField, make sure you use the correct ID of the element, which is going to be the Model name + underscore + attribute name. You can see the exact ID of the element by viewing source on your page. Here’s an example using jQuery:

<!-- form based on model named Model.... -->

<?php echo CHtml::activeHiddenField($model,'attribute_name'); ?>

<!-- ....end form.... -->

<script type="text/javascript">

$(document).ready(function() {

    var myVar = 'something';




perfect, that did the trick. (it also helps to view generated source)

thanks for your quick reply.