syrkull
syrkull

Reputation: 2344

function in javascript not working when called

I have a javascript code that works by removing the first and the last line of it.

Please take a look at JSFiddle

for people who wants to see it in here, here is my html:

<input id="search" onclick="search()" type="button" value="Search"/>

my javascript :

    function search() {
var search = document.getElementById('search');

var int = setInterval(function() {
    if (search.value.length == 6)
        search.value = 'Searchi';
    else if (search.value.length == 7)
        search.value = 'Searchin';
    else if (search.value.length == 8)
    search.value = 'Searching';
    else {
        search.value= 'Search';
            }
    //clearInterval( int ); // at some point, clear the setInterval
}, 500);

}

I want the function to work only when I click the button.

Upvotes: 0

Views: 53

Answers (1)

Alnitak
Alnitak

Reputation: 339985

You've selected jQuery in jsfiddle.net which by default causes the site to wrap your whole code in a document.ready handler.

The result is that your search function becomes a local function within that wrapper, and not a global variable as required by a DOM0 onclick handler.

Set the jsfiddle options to "no wrap (body)" and "No-Library (pure js)" to turn off that functionality.

Upvotes: 2

Related Questions