0

I am very new to programing so sorry if this question is to vague.

I have a simple html input field: input class="draft" type="text"

and I have a javascript function (the function only accepts strings): Message.send()

Using javascript/jQuery how do I take the string the user typed into the input field and place it into the Message.send() function (in string form)?

Thanks in advance for any help

Tyler7498
  • 3
  • 4
  • use the `value` property in pure JS or `val()` method in jQuery. – King King May 24 '14 at 03:01
  • 1
    Firstly: Welcome to StackOverflow. Secondly: Try searching for your answer prior to posting a question. Questions with simple answers like this one can often be found very easily. – MattSizzle May 24 '14 at 03:05

4 Answers4

0

jQuery:

Message.send($("input.draft").val());

javascript:

Message.send(document.querySelector("input.draft").value);
Elvis D'Souza
  • 2,273
  • 1
  • 23
  • 31
  • It is better to flag as duplicate then provide an answer. – MattSizzle May 24 '14 at 03:06
  • First off thank you very much Elvis D'Souza for the answer it did exactly what I needed it to! Secondly Matt Green I spent that evening searching google and stack overflow without finding an answer (or at least one I could understand). What would I search for to find an answer to this question? Im not trying to be smart just trying to learn so next time I can figure it out myself. Thanks – Tyler7498 May 25 '14 at 01:02
0

No jQuery needed. I'm not sure what you mean by place it into the Message.send() function (in string form), so I assume you get the value in the text input and use it as an argument in the Message.send() function.

Try this:

Message.send(document.getElementsByClassName("draft")[0].value);
chris97ong
  • 6,870
  • 7
  • 32
  • 52
0

Assuming your input field is something like this:

<input type="text" id="someInputField" name="someInputField" class="inputFields" />

You could do this

<script type="text/javascript">
   //We use the field's id to refer to it and get to its value (the text in it):
   var message = $('#someInputField').val();
   //And then you might call the function like:
   nameOfTheFuntion(message);
</script>

You would need to have jQuery libraries to make it work though, but you could do without them by replacing:

$('#someInputField').val();

with

document.getElementById('someInputField').val();
arrigonfr
  • 742
  • 3
  • 12
  • 34
0

Give your <input> box an id for example

<input type="text" id="blah" />

In your javascript you are able to reference the <input> like so:

var strBlah = document.getElementById("blah").value;

Now you have the value typed into the <input> box so you would do the following:

Message.send(strBlah)
AssemblyX
  • 1,841
  • 1
  • 13
  • 15