Search code examples
javascriptjqueryurlcontains

Replace url if there is a certain word is written in a textarea


I wanted to redirect the user on another url when I type a certain word in a textera. I tried something but it doesn't work. I don't have a lot of knowledge in javascript and jquery so could someone check my code?

var url = "https://myURL";      

$('.button').on('click', function() {
    if ($("textarea").is(':contains("test")'))
        $(location).attr('href',url);
    else
        alert("Wrong word !");
});


Solution

  • in this can search or indexOf method can be used as follows:

    var url = "https://myURL";      
    
    $('.button').on('click', function() {
        var introString = $("#introduction").val();
        if(introString.indexOf("test") >= 0){
          $("#location").attr('href',url);
        }else{
          alert("Wrong word !");
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <textarea id="introduction"></textarea>
    <button class="button">Submit</button>
    <a href="javascript:void(0)" id="location">Browse</a>