Tim Tim - 1 month ago 14
Javascript Question

Set Hidden Field value to Javascript Variable using Jquery or Javascript

Very Brief Background:

I am using Jquery Autocomplete to lookup the the value of an item from a database. That value is then somehow given to a hidden field within the same form and then inserted to the database.

What complicates this slightly is that I am working through Jquery Ui Tabs, which I haven't had a lot of fun with in the past.

So some code within the file that creates the tabs:

<script type="text/javascript">
function findValue(li) {
// if( li == null ) return alert("No match!");

// if coming from an AJAX call, let's use the CityId as the value
if( !!li.extra ) var sValue = li.extra[0];

// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;

function selectItem(li) {

function formatItem(row) {
return row[0];

function lookupAjax(){
var oSuggest = $(".role")[0].autocompleter;


return false;

function lookupLocal(){
var oSuggest = $("#role")[0].autocompleter;


return false;

The same file creates the tabs and also has a callback initiating the Jquery Autocomplete

<script type="text/javascript">
$(function() {
load: function(event, ui) { setTimeout( function() { $(".title").focus(); }, 500 );
var ac = $(".role").autocomplete(


Then in the actual tab code is the form

<?php $tab_id = $this->uri->segment(4);
$hidden = array('tab_id' => $tab_id);
$attributes = array('name' => 'additem');
echo form_open('profile/new_item', $attributes, $hidden); ?>
<input type="hidden" value="" name="rolehidden"/>
<?php echo validation_errors(); ?>
<table width="100%" padding="0" class="add-item">
<td>Title: <input class="title" type="text" name="title" value="<?php echo set_value('title'); ?>"></input></td>
<td>Role: <input class="role" type="text" name="role" size="15"></input></td>
<td>Year: <input type="text" name="year" size="4" maxlength="4"></input></td>
<td align="right"><input type="submit" value="Add"></input></td>

All I want to do is get sValue and make it the value of the hidden field in the form.

I should also mention that the JQuery Tabs have multiple tabs that all have the same form. This means that there are several different input fields all with the same name/id/class across all of the tabs.

I know this is a problem for ID attributes, but not sure if the same applies to the name attributes.

I have tried so many different code snippets of Javascript and Jquery that I just can't think anymore.

BREAKTHROUGH ... But still a problem


Just had a break through. This code does work... BUT only on an
<input ="text">
Element. It fails to work on
<input ="hidden">
Is there a work around or should I use CSS to hide the text input box?

Please help



"All I want to do is get sValue and make it the value of the hidden field in the form."

Have you tried?