Get value from input text in javascript

Asked

Viewed 55 times

1

Colleagues, I have the following scenario:

I have an input

input type="text" id="myField3" name="myField3" ng-model="selected"/>

I need to play the content of this input in a variable in javascript:

<script>
var escopo = document.getElementById('myField3').value;
console.log(escopo);
</script>

Does not return anything in the variable log. The input content is correct.

inserir a descrição da imagem aqui

  • Looks like a typo (or input is generated at another time in the program stream). No ) at the end of document.getElementById('myField3').value;

  • Do you want to take this value from outside of Angularjs, is that it? Because from within it will be updated in real time in the model selected.

  • Exactly I need to pick it out and store it in a variable. The input is coming from the angular and is ok. now I need the content of this input to play for a variable in javascript

  • Attaches the code with the folders in Google driver and provides the link here.

  • @It would be better to put a minimum, complete and verifiable example right here.

1 answer

-1

Uses the property val jquery.

Example:

$('#idDoInput').val();//ira retornar o conteudo do input.
  • Good morning friend.If you tested by Jquery I believe you have made a typo. Well your code is correct.I noticed that the input is missing one(<). I will post a code maybe it will help .

  • $(Function() { $('#boot'). click(Function() { w=Document.getElementById("textarea").value; Alert(w); }); ; });

  • If you continue with the error post the complete code

  • Friend still remains so. to attempt to illustrate by placing : var scope1 = angular.element(Document.getElementById('myField3')); on the console.log appears to me as follows: [input#myField3.ng-pristine.ng-untouched.ng-Valid, context: input#myField3.ng-pristine.ng-untouched.ng-Valid] e [0] inside "[0]" I have the attributes including the value I need

  • Boy! I tested your code here and it’s 100% OK. Instead of using the console.log to view the value of the variable use the alart();

  • 1

    @user10111111 Please do not edit the post with comments, that is not the purpose of the EDIT button. If the solution did not work, leave a comment as you have already done and have patience until the author of the reply to you.

  • Thiago, I appreciate the attention but it is not the case the problem is when I am passing the content of the input until the javascript var. This is necessary as I will use via JSON.stringify. In short, I just need the input content to be read in the js variable, and with the angular.element command (Document.getElementById('myField3')); it is falling into a kind of object, which inside the array[0] I have the attribute

  • is coming correct with the input contents tried for example like this but did not work angular.element0;

  • exactly, console.log(scope) *scope var scope = angular.element(Document.getElementById('myField3'));

  • tried the scope[0]. value but came blank

  • also did not come blank that strange...logically is like this so...

  • One last try tries this $("input[name='myField3']"). each(Function() { Alert($(this). val(); });

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.