Zeretil
Zeretil

Reputation: 297

I can't use val()?

I'm having a problem with getting the val of an input element I have. You see, I don't know if my code is wrong, but my Visual Studio (legal) doesn't even try to help me complete what I want to type. All it gives me is Value() and ValueOf().

The part of the code I'm using:

JS:

$(document).ready(start);
{
    $("#b1").click(toev);
}
function toev() {
    var value = $("#b1").val();
    $("#output").append(value);
};

HTML:

<input type="text" id="output"/>
<td><input type="button" id="b1" value="1" /></td>

Upvotes: 1

Views: 304

Answers (2)

Tats_innit
Tats_innit

Reputation: 34117

Demo http://jsfiddle.net/yS8tw/

Few things to note:

  • Use of document.ready
  • Use of Val

Also I like this appendVal function: http://jsfiddle.net/5R7eZ/ - Is it possible to do ".value +=" in JQuery?

Rest should fit the needs :)

code

$(document).ready(function(){
      $("#b1").click(toev);
  });

  function toev() {
      var value = $("#b1").val();
      alert(value);
      $("#output").val(value);
  }

With AppendVal

 $(document).ready(function(){
      $("#b1").click(toev);
  });

  function toev() {
      var value = $("#b1").val();

      $("#output").appendVal(value);
  }

 $.fn.appendVal = function (newPart) {
   return this.each(function(){ this.value += newPart; });
 };

Upvotes: 3

Pointy
Pointy

Reputation: 414016

You can't append content to an <input> element because it cannot have content. Perhaps you meant

  $('#output').val(value);

to set its value.

Upvotes: 0

Related Questions