0

HTML

<div id="wrapper">
<p>This is a basic tag input: 
    <input id="tag1" value="alpha,beta,gamma" />
</p>
</div>
<textarea id="tagdetect"></textarea>

script

$("#tagdetect").live("keydown",function(){

    var textTyped=$("#tagdetect").val();
    var text=textTyped.split(" ");
    for(i=0;i<text.length;i++){

        if(text[i]=="sql"||text[i]=="html"||text[i]=="java"){

            $("#tag1").val()=text[i];
        }
    }
});

what i am trying to achieve

I am trying to add tags(ie values) in that input field when certain keywords are detected

Working version

http://jsfiddle.net/7aDak/4839/

1
  • .live is deprecated. use .on Commented May 19, 2014 at 16:52

4 Answers 4

2

You have an error in your jQuery syntax.

Change the following line

$("#tag1").val()=text[i];

To this:

$("#tag1").val(text[i]);
Sign up to request clarification or add additional context in comments.

2 Comments

Try changing .live("keydown" to .on("keyup". Here's an updated fiddle: jsfiddle.net/p223B/2. Keyup will fire as soon as the user finishes entering the string. Keydown fires the function each time you press a key, so you would press a key once more, such as return, for keydown to work.
With that tagsInput function, it's hiding the #tag1 input. you'll need to use inspector or firebug to unhide. Not sure what else you're trying to do here, but the syntax error was the only problem.
0

to set the value you should use:

 $("#tag1").val(text[i])

Comments

0

Function .val() need to have parameters of what you want to append:

var value = $("#tag1").val();
$("#tag1").val(value + text[i]);

Comments

0

I think this is more elegant apart from fixing your errors - assuming you want all the tags in the field

var types = ["sql","html","java"];

$("#tagdetect").on("keydown",function(){

  var textTyped=$("#tagdetect").val();
  var text=textTyped.split(" ");
  $.each(text,function(_,txt) {
    if ($.inArray(txt,types)!=-1) $("#tag1").append(txt+" ");
  });
});

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.