-1

Is there any way to search a specific part on my input with Javascript/jQuery?

Tried to do with 2 different ways, but no results for that

<script type="text/javascript">
$("#button").click(function () {

    $("#DivToToggle").toggle();
    var campo = document.getElementById('FieldToSearch');

    if (campo.contains("Test")) {
        document.getElementById('FieldToWrite').value = "123";
    }
    else {
        document.getElementById('FieldToWrite').value = "456";
    }
});

and

<script type="text/javascript">
$("#button").click(function () {

    $("#DivToToggle").toggle();
    var field = document.getElementById('FieldToSearch');

    if (field.match(/Test.*/)) {
        document.getElementById('FieldToWrite').value = "123";
    }
    else {
        document.getElementById('FieldToWrite').value = "456";
    }
});

@EDIT

Thanks to @TalhaAbrar, found the correct way to do it.

<script type="text/javascript">
$("#button").click(function () {


    var field = document.getElementById('FieldToSearch').value;

    if (field.indexOf("Test")) {
        $("#DivToToggle").toggle();
        document.getElementById('FieldToWrite').value = "123";
    }
    else {
        $("#DivToToggle").toggle();
        document.getElementById('FieldToWrite').value = "456";
    }
});

Hudson Medeiros
  • 301
  • 2
  • 11
  • 3
    The problem is `FieldToSearch` is an HTML element - did you want the `text` property of it? – tymeJV Jul 18 '19 at 14:44
  • You can find answer to your question [here](https://stackoverflow.com/questions/5324798/how-to-search-an-array-in-jquery-like-sql-like-value-statement). https://stackoverflow.com/questions/5324798/how-to-search-an-array-in-jquery-like-sql-like-value-statement – Talha Abrar Jul 18 '19 at 14:45
  • 1
    @TalhaAbrar not really. That's about arrays, not Element objects – Rory McCrossan Jul 18 '19 at 14:46
  • @tymeJV Yes, i want to search some text in this input. If have, do something. Else, do another thing – Hudson Medeiros Jul 18 '19 at 14:49
  • 1
    You need the `value` of that element when it is a form control – charlietfl Jul 18 '19 at 14:50
  • @HudsonMedeiros Try to fetch the value of search field like `document.getElementById('FieldToSearch').value` and then use function `indexOf` in your if statement, that should work for you. – Talha Abrar Jul 18 '19 at 14:58
  • Thanks to @TalhaAbrar! It works like a charm. Edited main post with functional code. – Hudson Medeiros Jul 18 '19 at 15:13
  • @HudsonMedeiros, glad to be of help, i have added my comment as an answer, kindly accept it as answer so that others can benefit from it as well – Talha Abrar Jul 18 '19 at 17:00

2 Answers2

0

I think you need to check the value of the field, e.g.

var campo = document.getElementById('FieldToSearch').value;
...

And then use includes(), e.g.

if (campo.includes("Test")) {
...
Chris Wheeler
  • 1,623
  • 1
  • 11
  • 18
0

Try to fetch the value of search field like document.getElementById('FieldToSearch').value and then use function indexOf in your if statement, that should work for you.

example code:

var field = document.getElementById('FieldToSearch').value;

if (field.indexOf("value_to_check")) {
    // true if exists
} else {
    // false if doesn't
}
Talha Abrar
  • 880
  • 8
  • 22