Vinay T
Vinay T

Reputation: 1

How to Pass the Text field value to a variable in Javascript

i have the below script where i want to pass the text field value to a variable and display in alert window

    <form name='searchdata' id="search_form" >
    <div class="col-xs-4" id="form_submit_ins">
    <label for="usr">Search Instance</label>
    <input type="text" class="form-control" id="ins" id"searchval" placeholder="Instance Name"><br/>
    <button class="btn btn-danger navbar-btn" onclick="form_submit();" >Search</button>
    </div>
    <div>

    </div>
    </form>
    </div>
     <script type="text/javascript">
      function form_submit() {
      var item = document.getElementById('searchval').value;
        alert(item);

    }

Upvotes: 0

Views: 350

Answers (4)

user7332411
user7332411

Reputation:

Your input has two id's, it can only have one id

change:

<input type="text" class="form-control" id="ins" id"searchval" placeholder="Instance Name">

To:

<input type="text" class="form-control" id="searchval" placeholder="Instance Name">

Upvotes: 0

Vexen Crabtree
Vexen Crabtree

Reputation: 359

There is something wrong with your HTML (does it have two ID fields? Is one missing a "=" sign?

Your code should work if you fix the HTML to:

<input type="text" class="form-control" id="searchval" placeholder="Instance Name">

Upvotes: 0

Mohit Bajoria
Mohit Bajoria

Reputation: 505

id should be equal to searchval i.e id="searchval"

Upvotes: 0

Rob M.
Rob M.

Reputation: 36511

You have a typo:

id"searchval"

Should be:

id="searchval"

Upvotes: 2

Related Questions