Check if TextArea/TextBox contains a certain String

前端 未结 3 680
无人共我
无人共我 2020-12-20 08:52

Given textarea is a textarea element with id \'textareabox\', how can I check if it contains a string within it?

var textarea = document.getElementById(\'tex         


        
3条回答
  •  悲&欢浪女
    2020-12-20 09:30

    You can use .value, as:

    var textarea = document.getElementById('textareabox');
    
    var word = 'something';
    
    var textValue=textarea.value;  //-> don't use .innerHTML since there is no HTML in a textarea element
    
    if (textValue.indexOf(word)!=-1)
    {
      alert('found')
    }
    

提交回复
热议问题