0

This is my first html textbox with javascript code

<div>
<input type="text" onchange="getTheValue(this)" id="12345" />
</div>

and this is my second html tag

<input type="hidden" id="12345_hiddenField" ng-model="hiddenField"/>

And on onchange event i am calling this javascript function

function getTheValue(data) {
document.getElementById("12345_hiddenField").value = data.value;
}

i want first textbox value to be assigned to second textbox ngmodel value with pure javascript no angualrjs methods in that, becoz that onchange function is written in seperate pure javascript file,is their anyway to do this?

3
  • What if i told you you don't need an onChange method ? Is that ok for you ? Commented May 13, 2016 at 11:41
  • You can remove the ng-model tag from the second input field. Commented May 13, 2016 at 11:42
  • yes its ok, all i need is that eventually i want that first textbox value to be assigned to ngmodel value and even it should watch that. Commented May 13, 2016 at 11:43

2 Answers 2

1

I would do it like this.

 document.getElementById("12345").addEventListener('change',function() {
 document.getElementById("12345_hiddenField").value = document.getElementById("12345").value;

See fiddle https://jsfiddle.net/9fh8rxck/

Sign up to request clarification or add additional context in comments.

4 Comments

is their anyway to add that value to ngmodel?
document.getElementById("12345").setAttribute("ng-model", "YourValue") But ofcourse i don't know if ng-model is visible when rendered to html. So I would suggest that you use a data-value in your html input like this. <input type="hidden" id="12345_hiddenField" ng-model="hiddenField" data-value="empty"/> and then you document.getElementById("12345_hiddenField").setAttribute("data-value", "YourValue")
is their anyway to watch that 'data-value'?
Depends on what you mean by watch? You can always right-click on your site and select "Inspect Element" and then locate your hidden field in the html, here you can watch the data-value. Btw. you can call it whatever you want, just as long as you call i data- , so it could be data-tempvalue or data-myvalue etc. But if you want to get the value then you just say document.getElementById("12345_hiddenField").getAttribute("data-value");
0

<input type="text" ng-model="textboxValue" id="12345" />

and your hidden:

<input type="hidden" id="12345_hiddenField" value="{{textboxValue}}"/>

Your input text is bound to the $scope property textboxValue

Your hidden input uses that variable as value {{textboxValue}}, everything is 2-way data-bound.

3 Comments

but i should not have that first textbox in angualrjs format, instead second textbox has angularjs code, what i want is value inside of textbox changes i want to watch that and assign it to ngmodel
The function you're calling, is it within a separate js file ? Is it within angularjs ?
the function which i am calling is pure javascript file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.