0

I have two seperate Javascript/jQuery scripts which I would like to combine. One is a tabbed search box script which determines the destination of the form when it is submitted. The other is a simple Javascript search script.

I want to combine the two scripts so that the tabbed script determines the place to pull content from in the search script. I hope people can understand what I am trying to describe.

My Javascript search script is:

$(document).ready(function(){
    $("#query").keyup(function(e){
        if(e.keyCode==13){
            var query=$(this).val();
            var yt_url='search.php?q='+query;
            window.location.hash='search/'+query+'/';
            $.ajax({
                type:"GET",
                url:yt_url,
                dataType:"html",
                success:function(results){
                   $('#results').html(results);
                }
            });
        }
    });
});

My tabbed search script is:

$(document).ready(function () {
    Tabs.types.init('search');
    Tabs.search.init();
});

var Tabs = {
    search: {
        init: function () {
            jQuery(Tabs.element.form).bind('submit', function (evt) {
                evt.preventDefault();
                Tabs.search.submit();
            });
        },
        submit: function () {
                var type = Tabs.types.selected;
                var url = type;
                window.location.href = url;
        },
    },
    types: {
        init: function (selected) {
            Tabs.types.selected = selected;
            jQuery('.' + Tabs.types.selected).addClass('selected');
            jQuery(Tabs.element.types).bind('click', function () {
                Tabs.types.click(jQuery(this));
            });
        },
        click: function (obj) {
            jQuery(Tabs.element.types).each(function () {
                if (jQuery(this).hasClass('selected')) {
                    jQuery(this).removeClass('selected');
                }
            });
            if (obj.hasClass('web')) Tabs.types.selected = 'search';
            if (obj.hasClass('images')) Tabs.types.selected = 'images';
            if (obj.hasClass('videos')) Tabs.types.selected = 'videos';
            if (obj.hasClass('news')) Tabs.types.selected = 'news';
            if (obj.hasClass('social')) Tabs.types.selected = 'social';
            obj.addClass('selected');
        }
    },
    element: {
        types: '.type',
        form: '#search',
    },
};

1 Answer 1

1

You can get the selected tab from Tabs.types.selected and know it on your query. For example

$(document).ready(function(){
    $("#query").keyup(function(e){
        if(e.keyCode==13){
            var query=$(this).val();
            var yt_url='search.php?tab=' + Tabs.types.selected + '&q=' + encodeURIComponent(query);
            window.location.hash='search/'+query+'/';
            $.ajax({
                type:"GET",
                url:yt_url,
                dataType:"html",
                success:function(results){
                   $('#results').html(results);
                }
            });
        }
    });
});

I suggest to encode your query with encodeURIComponent, or else if your user type & and ? and other symbols, then never reach your page as parametres.

Also place the var Tabs = { search: { ..etc before this code so you are sure that the Tabs are found.

Sign up to request clarification or add additional context in comments.

1 Comment

@callum this is a different issue. Do you wish to work both search, or only one of them ? The line that do that is here Tabs.search.submit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.