-1

I'm creating a search bar using jQuery and my code is pasted below

Fiddle : https://jsfiddle.net/fpLf1to4/

var inputSearch = $('.searchInput').hide(); 
var searchArray = ['s','e','a','r','c','h'];
var searchReturn = '';
$('#search').on('click', function(){
 $(inputSearch).fadeToggle(500).focus();
 for(var i = 0 ; i < searchArray.length ; i++){
 searchReturn = searchReturn + searchArray[i];
 } 
 $(inputSearch).attr('placeholder', searchReturn[0]);
});

Note : now I'm using only the first index of the array for my input attribute.

$(inputSearch).attr('placeholder', searchReturn[0]);

Now after every one second I want my attr() to be updated like

$(inputSearch).attr('placeholder', searchReturn[0] + searchReturn[1]);

and the very next second

$(inputSearch).attr('placeholder', searchReturn[0] + searchReturn[1] + searchReturn[2]);

and so on ...

Wowsk
3,6852 gold badges20 silver badges30 bronze badges
asked Sep 6, 2015 at 11:24
1
  • Use setTimout to add the following letters Commented Sep 6, 2015 at 11:28

3 Answers 3

1

You have to use setInterval function, instead of for loop. Here is how you can do it:

$('#search').on('click', function(){
 $(inputSearch).fadeToggle(500).focus();
 var i = 0; 
 var interval = setInterval(function() { 
 searchReturn = searchReturn + searchArray[i]; 
 $(inputSearch).attr('placeholder', searchReturn);
 if(i == searchArray.length - 1) {
 clearInterval(interval) 
 }
 i++;
 }, 2000); 
 });

And here is the fiddle: https://jsfiddle.net/fpLf1to4/1/

answered Sep 6, 2015 at 11:36
Sign up to request clarification or add additional context in comments.

Comments

1

Try utilizing .queue() , setTimeout

 var inputSearch = $('.searchInput').hide();
 var searchArray = ['s', 'e', 'a', 'r', 'c', 'h'];
 $('#search').on('click', function() { 
 inputSearch.queue("s", [])
 .attr("placeholder", "")
 .fadeToggle(500).focus()
 .queue("s", $.map(searchArray, function(value) {
 return function(next) {
 $(this).attr("placeholder", function(_, p) {
 return p === undefined ? value : p + value
 });
 setTimeout(next, 1000)
 }
 })).dequeue("s")
 });

jsfiddle https://jsfiddle.net/fpLf1to4/5/

Comments

1

Is this what you wanted? You can change the timeout if you want execution to be faster or slower.

var inputSearch = $('.searchInput').hide(); 
var searchArray = ['s','e','a','r','c','h'];
var searchReturn = '';
$('#search').on('click', function(){
 $(inputSearch).fadeToggle(500).focus();
 updateSearch(); 
});
var currentIdx = 0;
function updateSearch() {
 var placeholder = $(inputSearch).attr('placeholder')
 if (placeholder === undefined) {
 placeholder = "";
 }
 placeholder += searchArray[currentIdx];
 $(inputSearch).attr('placeholder', placeholder);
 currentIdx++;
 if (currentIdx >= searchArray.length) {
 return;
 }
 setTimeout(updateSearch,500);
}

https://jsfiddle.net/fpLf1to4/6/

answered Sep 6, 2015 at 11:40

1 Comment

Update your fiddle link.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.