1

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>
Rudie
  • 52,220
  • 42
  • 131
  • 173
Zeretil
  • 287
  • 2
  • 19
  • `$("#output").val(value)`? – Venkata Krishna Dec 11 '13 at 21:27
  • 3
    And `$(document).ready(start); { $("#b1").click(toev); }` is invalid or at least not what you mean. – Rudie Dec 11 '13 at 21:30
  • Others have pointed out the issues with your code, but to address "_Visual Studio (legal) doesn't even try to help me complete what I want to type_" -- Visual Studio doesn't always have perfect intellisense for javascript, and definitely not for jQuery, though 2012 is much improved. – Jason P Dec 11 '13 at 21:33

2 Answers2

3

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; });
 };
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • 1
    Thank you Tats! This helped me get it working! I was a bit confused with the $(document).ready part. I was thought it so fast (I'm in college) that it's all a bit much right now. Thank you again! And also to everyone else who answered. – Zeretil Dec 11 '13 at 21:37
  • @user3092948 Glad it helped you! Have fun! `:)` All it matters is to learn the basics, rest just flows in if you keen. You will be saweeet yo! – Tats_innit Dec 11 '13 at 21:38
  • Thanks for indirectly pointing out I didn't have an username! And yes indeed, but the problem is, right now in College we are busy covering the basics for Linux, C#, jquery (and javascript, html, css) all at once and it gets very confusing / hard to keep up with everything. But thanks for helping me again. I hope I can get this down before finals. – Zeretil Dec 11 '13 at 22:24
0

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

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

to set its value.

Pointy
  • 405,095
  • 59
  • 585
  • 614