sotiris hamzo sotiris hamzo - 4 months ago 23
Javascript Question

Uncaught TypeError: $(this).search is not a function

Uncaught TypeError: $(this).search is not a function

$(document).ready(function(){
$('#caption').on('keypress', function () {
var n = $(this).search('#');
if(n != "-1"){
window.alert("There's a hash");
}else{
window.alert("There's not a hash");
}


});
});

Answer

You can use indexof(), if you want search in your input the character #

 $(document).ready(function(){
    $('#caption').on('keypress', function () { 
        var n = $(this).val();
        if(n.indexOf("#") > -1){
            window.alert("There's a hash");
        }else{
             window.alert("There's not a hash");
        }

   });
});

Result: https://jsfiddle.net/cmedina/3v04fvmb/