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

2
  • use the value property in pure JS or val() method in jQuery. Commented May 24, 2014 at 3: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. Commented May 24, 2014 at 3:05

4 Answers 4

0

jQuery:

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

javascript:

Message.send(document.querySelector("input.draft").value); 
Sign up to request clarification or add additional context in comments.

2 Comments

It is better to flag as duplicate then provide an answer.
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
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); 

1 Comment

It is better to flag as duplicate then provide an answer.
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(); 

Comments

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.