0

Hi here I have a array variable links. I have to add the value for keywords and label dynamically. How do you add this? Thank you

update

MyIssue I need to add(push) the value for keyword and labels dynamically in that Array for autocomplete. How to do this?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete - Default functionality</title>
    <link rel="stylesheet" href="http://jqueryui.com/themes/base/jquery.ui.all.css">
     <script type="text/javascript" charset="utf-8" src="../js/cordova-1.7.0.js"></script>
 <script type="text/javascript" charset="utf-8" src="../js/jquery-1.7.2.js"></script>

    <script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
    <script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
    <script src="http://jqueryui.com/ui/jquery.ui.position.js"></script>
    <script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"></script>
    <link rel="stylesheet" href="http://jqueryui.com/demos/demos.css">
    <script>
    $(function() {
        var links = [
        {
            keywords: ['create', 'add', 'make', 'insert', 'user'],
            label: "Create user",
            //desc: "Create a user in the system",
            //url: 'http://mysite.com/user/create/'
        },
        {
            keywords: ['create', 'add', 'make', 'insert', 'organisation'],
            label: "Create organisation",
           // desc: "Create an organisation in the system",
           // url: 'http://mysite.com/organisation/create/'
        }];
        $( "#tags" ).autocomplete({

             source: function(request, response) {  
             var matched = [];

        for (var k = 0; k < links.length; k++) {
            if (checkSearchWordsMatchKeywords(request.term, links[k]['keywords'])) {
                matched.push(links[k]);
            }
        }
        // display the filtered results
        response(matched);

             } 
        });

        function checkSearchWordsMatchKeywords(searchWords, keywords)
        {
            var searchWords = searchWords.toLowerCase();    // Lowercase the search words
            var searchWords = searchWords.split(' ');       // Break up the search into separate words
            var numOfSearchWords = searchWords.length;      // Count number of search words
            var numOfKeywords = keywords.length;            // Count the number of keywords
            var matches = [];                               // Will contain the keywords that matched the search words

            // For each search word look up the keywords array to see if the search word partially matches the keyword
            for (var i = 0; i < numOfSearchWords; i++)
            {
                // For each keyword
                for (var j = 0; j < numOfKeywords; j++)
                {   
                    // Check search word is part of a keyword
                    if (keywords[j].indexOf(searchWords[i]) != -1)
                    {
                        // Found match, store match, then look for next search word
                        matches.push(keywords[j]);
                        break;
                    }
                }
            }
            if (matches.length == numOfSearchWords)
            {

                return true;
            }
            }

    });
    </script>
</head>
<body>

<div class="demo">

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>

</div><!-- End demo -->

</body>
</html>
5
  • i need to add the value dynamically for keywords and label Commented Aug 1, 2012 at 12:49
  • How do you decide exactly which keyword array you want to add values into? Commented Aug 1, 2012 at 12:50
  • i have some value in database for keyword and label.Is it possible to add the value? Commented Aug 1, 2012 at 12:53
  • Yes it is possible :) .. See there are two keywords arrays in your question, my question is, how do you decide in which array you want to add new value? Commented Aug 1, 2012 at 12:55
  • for example here i have value ['create', 'add', 'make', 'insert', 'user'] is "0th position" in keywords similar that in label: "Create user" is "0th" position...i need to add like that Commented Aug 1, 2012 at 13:04

3 Answers 3

2

General way is (provided you know the index of keyword array),

links[index].keywords[links[index].keywords.length] = "your new value";

and

links[index].label = "your new label value";

Let us know how do you determine index value,so that we can help you in more specific way :)

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

2 Comments

Thank you for reply ...index starts from zero ,,links[index].keywords[links[index].keywords.length] = "your new value"; i dont understand this line
Its like, links[index].keywords will select the keyword array... links[index].keywords[links[index]. Keywords.length ] will give you the length of that selected array. And you will add the new element at the end if that keywords array...
2

to add a new label

newLabel = { "keywords":[], "label":"Empty Label" };
links.push(newLabel);

to add to keywords, you will need to iterate through the links array

$(links).each(function(){ if(this["label"] = "Empty Label") { this["keywords"].push("newKeyword") } });    

the above code is considering you want to add to keywords of the "Empty Label" Label

Comments

0

Set the keys and label as variables, then add the keys to the label's index in the array:

var links = new Array();
var keys = ['create', 'add', 'make', 'insert', 'user'];
var label = "Create user";

links[label] = keys;

alert(links["Create user"]);

The alert: create,add,make,insert,user

Comments

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.