user3501407
user3501407

Reputation: 457

html form without submit button and anchor link onclick redirect with input value

I want to redirect people like this: example.com/search/[search+text]

html source code:

<form name="menu_search" method="get" onsubmit="window.location.href = 'http://www.col3negmovie.com/search/'+this.q.value+''; return false;" >
    <input name="q" id="videoSearch" />
</form>

this is the anchor link :

<a href="./#" onclick="document.menu_search.submit(); window.location.href='http://www.col3negmovie.com/search/'+this.q.value+''; return false;">Search</a>

onclick doesn't work, how could i fix this?

onclick="document.menu_search.submit();
window.location.href='http://www.col3negmovie.com/search/'+this.q.value+'';
return false;"

Upvotes: 0

Views: 1337

Answers (1)

chris97ong
chris97ong

Reputation: 7060

Your form:

<form name="menu_search" method="get" onsubmit="window.location.href = 'http://www.col3negmovie.com/search/'+document.getElementsByName('q')[0].value+''; return false;" >
<input name="q" id="videoSearch" />
</form>

Anchor tag:

<a href="./#" onclick="document.getElementsByName('menu_search')[0].submit(); return false;">Search</a>



Or without the <form> tag (which makes it less confusing):

<input name="q" id="videoSearch" />
<a href="./#" onclick="window.location.href = 'http://www.col3negmovie.com/search/' + document.getElementById('videoSearch').value;">Search</a>

To do the same when 'Enter' is pressed:

document.getElementById("videoSearch").onkeyup = function() {
if (this.event.keyCode == 13 || this.event.which == 13)
    //Enter was pressed, handle it here
    window.location.href = 'http://www.col3negmovie.com/search/' + document.getElementById('videoSearch').value;
}
}

Hope that helped.

Upvotes: 2

Related Questions