Check the code bellow. When i enter some keyword in input field and press Search
button its successfully redirecting to youtube url but if i press enter then this not redirecting however i used .click()
method for enter press. How can i fix it? I want redirection on press enter key from keyboard and also press search button. How can i fix it?
$(document).ready(function () {
$(document).on("keypress", function(e){
$("#srcBtn").click();
});
$('#srcBtn').on('click', function () {
var srcValue = $('#srcValue').val();
var youtubeUrl = 'https://www.youtube.com/results?search_query=';
//console.log(srcValue);
window.open(youtubeUrl+srcValue,"_self");
});
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<form method="get" action="">
<br/>
<br/>
<br/>
<div style="text-align:center;">
<input type="text" class="form-group form-control input-lg" id="srcValue" value="" placeholder="Search YouTube">
<button type="button" class="btn btn-info btn-lg" id="srcBtn">Search</button>
</div>
</form>
</div>