views:

1834

answers:

2

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) {
    findValue(li);
}

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

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

    oSuggest.findValue();

    return false;
}

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

    oSuggest.findValue();

    return false;
}
</script>

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

<script type="text/javascript">
        $(function() {
            $("#tabs").tabs({
             load: function(event, ui) { setTimeout( function() { $(".title").focus(); }, 500 );
                    var ac = $(".role").autocomplete(
                        "/profile/autocomplete",
                        {
                            delay:10,
                            minChars:1,
                            matchSubset:1,
                            matchContains:1,
                            cacheLength:10,
                            onItemSelect:selectItem,
                            onFindValue:findValue,
                            formatItem:formatItem,
                            autoFill:true
                        }
                    );

                    ac[0].autocompleter.findValue(); 
                    }
                });
            });
        </script>

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">
    <tr>
        <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>
    </tr>
</table>
</form>t 

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

$("[name='rolehidden']").val(sValue);

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

Tim

+2  A: 

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

Have you tried?

$("[name='rolehidden']").val(sValue);
Jonathan Sampson
Where should I put this? Do i have to trigger this on the submit button??? Sorry i need a little more help
Tim
Put it at the end of your `findValue()` function. That's where `sValue` is created, from what I can tell.
Jonathan Sampson
Hello,Yes I have tried this, and just tried again however it doesnt work.Please note I have added more info that may be crucial for this to work...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.
Tim
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?
Tim
@Tim, it will work for a `hidden` input too. Post your "non-working" example at http://jsbin.com and I'll take a look at it.
Jonathan Sampson
Yes, it does seem to be working now... but Firebug was not showing showing it working. This must be a Firebug Bug. Thanks for all your help
Tim
+2  A: 

Try $('input[name=rolehidden]').val(sValue);

Reid
Where should I put this? Do i have to trigger this on the submit button??? Sorry i need a little more help
Tim
Tried this, however it doesnt seem to work.
Tim