0

I will try to explain this the best i can. I have been working on a script for generating tags and hidden form input fields and have progressed quite far thanks to Stack users and google (i'm a js/jquery newbie). The desired behavior is for the user to select a search type, enter a value with bootstrap typeahead assisting to ensure that we get the correct values, and then generate a tag and input when the add button is clicked or the input field detects "enter". This is all working correctly except that after the first tag/input is created the value for the next remains the same as the first. this is the complete script:

HTML markup:

<div class="quick-search row">
    <div class="span7">
        <div class="input-append input-prepend">
            <div class="btn-group">
                <button id="search-type" class="btn btn-inverse dropdown-toggle" data-toggle="dropdown">
                    Search by
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li><a data-name="cat" data-label="type">Property Type</a></li>
                    <li><a data-name="lowprice" data-label="low price">Low Price</a></li>
                    <li><a data-name="highprice" data-label="high price">High Price</a></li>
                    <li><a data-name="bdrms" data-label="bedrooms">Bedrooms</a></li>
                    <li><a data-name="bthrms" data-label="bathrooms">Bathrooms</a></li>
                    <li><a data-name="city" data-label="city">City</a></li>
                    <li><a data-name="zip" data-label="zip">Zip Code</a></li>
                </ul>
            </div>
            <input id="input-tag" class="span4" id="appendedPrependedInput" type="text" placeholder="Select a search type..." data-provide="typeahead" value="" />
            <button id="add-tag" class="btn btn-primary"><i class="fa fa-plus"></i></button>
        </div>
        <form id="search" class="form-inline" method="get" action="/property/search">
            <div id="alert"></div>
            <div id="tag-results"></div>
            <input class="btn btn-primary" name="Search" type="submit" value="Search Properties" title="Search Properties" />
            <button class="btn btn-success">Whats My Home Worth?</button>
        </form>
    </div>
</div>

part one is the array for the typeahead:

var obj = {
    "cat" : [
        {val: 'res', string: "Residential & Single Family Homes"},
        {val: 'Dup', string: "Duplexes & Apartments"},
        {val: 'Con', string: "Condominiums"},
        {val: 'Lot', string: "Lots, Land & Farms"},
        {val: 'Com', string: "Commercial"},
        {val: 'Mob', string: "Mobile Homes"}
    ],
    "city" : [
        <!-- TMPL_LOOP Cities -->
        {val: "<!-- TMPL_VAR city_name -->", string: "<!-- TMPL_VAR city_name -->"},
        <!-- /TMPL_LOOP -->
    ],
    "bdrms" : [],
    "bthrms" : [],
    "zip" : [],
    "lowprice" : [],
    "highprice" : [],
};
/*dynamic bedrooms*/
for(i=1; i<=5; i++){
    obj.bdrms.push({val: i.toString(), string: i.toString() + " - Bedrooms"});
}
/*dynamic fullbaths*/
for(i=1; i<=4; i++){
    obj.bthrms.push({val: i.toString(), string: i.toString() + " - Bathrooms"});
}
/*dynamic halfbaths*/
for(i=1; i<=4; i++){
    obj.bthrms.push({val: (i+.1).toString(), string: i.toString() + " - 1/2 Bathrooms"});
}
/*dynamic zipcode*/ 
for(i=43000; i<=45999;i++){
    obj.zip.push({val: i.toString(), string: i.toString()});
}
for(i=48000; i<=49999;i++){
    obj.zip.push({val: i.toString(), string: i.toString()});
}
/*dynamic lowprice*/
for(i=0; i<=1000000;){
    obj.lowprice.push({val: i.toString(), string: i.toString()});
    obj.highprice.push({val: i.toString(), string: i.toString()});
    i=i+5000;
}

part two is the handler for the typeahead:

/********************************
/ SEARCH TYPEAHEAD FUNCTION
/*******************************/
$(function searchTag($) {   

    var data = [];

    $('.dropdown-menu > li > a').on("click", function() {
        data = $(this).data('name');
    });

    var that = this;
    $('#input-tag').typeahead({
        source: function(query, process) {
                var results = _.map(obj[data], function(value) {
                    return value.val;
                });
                process(results);
        },
        highlighter: function(val) {
            var value = _.find(obj[data], function(p) {
                return p.val == val;
            });
            return value.string;
        },
        updater: function(val) {
            var value = _.find(obj[data], function(p) {
                return p.val == val;
            });
            that.setSelected(value);
            return value.string;
        }
    });
    this.setSelected = function(value) {
        $('#input-tag').val(value.string);
        $('#input-tag').attr('data-value', value.val);
    };
});

part three is the handler for generating the tag/inputs:

/********************************
/ QUICK SEARCH TAG FUNCTION
/*******************************/
$(function () {

    /*GLOBAL VARIABLES*/
    var id = null;
    var name = null;
    var length = null;
    var selected = false;
    var input = null;
    var value = null;

    /*STRINGS*/
    var caret = ' <span class="caret"></span>';
    var searchType = 'Search by <span class="caret"></span>';
    var defaultPlaceholder = 'Select a search type...';
    var criteriaWarning = '<div class="alert alert-error"><i class="remove dismiss fa fa-remove"></i><strong>Warning!</strong><small><br />Please enter your criteria.</small></div>';
    var typeWarning = '<div class="alert alert-error"><i class="remove dismiss fa fa-remove"></i><strong>Warning!</strong><small><br />Please select a search type.</small></div>';

    function searchSelect() {
        $('.dropdown-menu > li > a').on("click", function() {
            $('#search-type').html($(this).text() + caret);
            $('#input-tag').attr('placeholder', $(this).text());
            id= $(this).data('name');
            name= $(this).data('name');
            selected = true;
    });
    }searchSelect();

    function searchValue () {
        $('#input-tag').on("change", function() {
            value = $('#input-tag').data('value');
            length = $("#input-tag").val().length;
            input = $("#input-tag").val();
        });
    }searchValue();

    function createTag() {
        if((selected === true) && (length > 0)) {
            $('#tag-results').append('<div class="tag"><input id="'+id+'" type="hidden" name="'+name+'" value="'+value+'" />'+input+'<i class="remove dismiss fa fa-remove"></i></div>');
            $('#search-type').html(searchType);
            $('#input-tag').attr('placeholder', defaultPlaceholder).val('');
            $('#alert').empty();
        } else {
            if(selected === true) {
                $('#alert').append(criteriaWarning);
            } else {
                $('#alert').append(typeWarning);
            }
        }
    }

    $('#add-tag').on("click keypress", function() {
        createTag();
    });

    $('#input-tag').on("keypress", function(e) {
        var code = e.keyCode || e.which;
        if(code == 13) { // 13 = enter key-code        
                createTag();
        }
    });

});

The result is a duplicate value attribute:

<div id="tag-results">
    <div class="tag">
        <input id="cat" type="hidden" name="cat" value="res">
        Residential &amp; Single Family Homes<i class="remove dismiss fa fa-remove"></i>
    </div>
    <div class="tag">
        <input id="lowprice" type="hidden" name="lowprice" value="res">
        10000<i class="remove dismiss fa fa-remove"></i>
    </div>
</div>

and should be:

<div id="tag-results">
    <div class="tag">
        <input id="cat" type="hidden" name="cat" value="res">
        Residential &amp; Single Family Homes<i class="remove dismiss fa fa-remove"></i>
    </div>
    <div class="tag">
        <input id="lowprice" type="hidden" name="lowprice" value="10000">
        10000<i class="remove dismiss fa fa-remove"></i>
    </div>
</div>
4
  • jsfiddle.net/qb6h9739/1 Commented Jul 20, 2015 at 17:29
  • <div id="tag-results"><div class="tag"><input id="cat" type="hidden" name="cat" value="res">Residential &amp; Single Family Homes<i class="remove dismiss fa fa-remove"></i></div><div class="tag"><input id="lowprice" type="hidden" name="lowprice" value="res">100000<i class="remove dismiss fa fa-remove"></i></div></div> Commented Jul 20, 2015 at 17:33
  • should be: <div id="tag-results"><div class="tag"><input id="cat" type="hidden" name="cat" value="res">Residential &amp; Single Family Homes<i class="remove dismiss fa fa-remove"></i></div><div class="tag"><input id="lowprice" type="hidden" name="lowprice" value="100000">100000<i class="remove dismiss fa fa-remove"></i></div></div> Commented Jul 20, 2015 at 17:33
  • so the value of the generated input created by the searchValue function is not updating when a new input criteria is made. it is carrying the value from the previous instance. Commented Jul 20, 2015 at 17:35

1 Answer 1

1

You need to make change to the setSelected function

this.setSelected = function(value) 
{
        $('#input-tag').val(value.string);
        $('#input-tag').data('value', value.val);
};

This answer: using $.data("value") versus $.attr("data-value") on SO specifies why

 $('#input-tag').data('value', value.val); 

works and

$('#input-tag').attr('data-value', value.val);

doesn't.

Here is updated fiddle: http://jsfiddle.net/qb6h9739/2/

Hope this helps!

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

1 Comment

thanks for the answer and working on this. big thanks for a link with the explanation!

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.