user3802409
user3802409

Reputation:

Both jQuery and JavaScript returns blank text value

I am coding a website with bootstrap. I have a modal, it contains a tetxtbox and a button. I want when i click the button, an alert box gives me the textbox value. But there is a problem that i can not solve. Alert box shows nothing.

Like this:

enter image description here

My codes and tries:

index.html

<div class="form-inline" id="b"> 
     <input type="text" value="" class="form-control" id="textbox" placeholder="something">
     <button type="button" id="button1" class="btn btn-info">Button</button>
</div>

script.js

$("#button1").click(function(){     
   var x = $("#textbox").val();
   alert(x);            
});

I also tried this way in js file:

$("#button1").click(function(){     
   var x = document.getElementById("textbox").value;
   alert(x);            
});

Why can't I get the value? And how can I solve this problem?

Upvotes: 0

Views: 566

Answers (1)

Nadeemmnn Mohd
Nadeemmnn Mohd

Reputation: 724

Check the demo is this the thing you were looking for

http://jsfiddle.net/nadeemmnn2007/bn2crc28/

html

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
      <div class="modal-content">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
          <h4 class="modal-title" id="myModalLabel">Modal title</h4>
        </div>
        <div class="modal-body">
            <p><input type='text' id="textbox"/>
              <a href="#" id="test" role="button" class="btn btn-default popover-test" title="" data-content="And here's some amazing content. It's very engaging. right?" data-original-title="A Title">button</a> </p>


          <hr>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>

      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Launch demo modal
    </button>

jquery

$('#test').on('click',function(){
    alert($('#textbox').val());

});

note:include jquery and bootstrap as shown in demo

Upvotes: 1

Related Questions