1

I'm currently confuse in trying to obtain the value inside a text box using jquery .value

<input class="input referral-code" disabled="true" ng-model="account.referral_code_string" id="input-refcode">

I tried using document.getElementById('input-refcode').value to try to obtain the value but it comes out as empty on my console.

But if I tried binding it onto a on-click event, it manages to obtain the value stored inside the ng-model

$('.trigger').on('click', function(){
   console.log(document.getElementbyId('input-refcode'))
});

As you can see there is a value inside the text box itself. But when you tried inspecting it, this is what came out.

<input class="input referral-code" disabled="true" ng-model="account.referral_code_string" id="input-refcode">

If you look at there is no value inside the input textbox. I'm not sure if it has something to do with angular.js

enter image description here

4
  • 1
    getElementById <- capital "B" Commented May 16, 2017 at 4:49
  • When does your jQuery code execute? Does the element exist? Does it have text in it? Commented May 16, 2017 at 4:51
  • When does your jQuery code execute? After rendering the document Does the element exist? Yes Does it have text in it? Yes Commented May 16, 2017 at 4:57
  • You'll need to prove it. If the element exists, with that id and containing text, then document.getElementById('input-refcode').value should return that text Commented May 16, 2017 at 4:59

3 Answers 3

1

You can get the scope object using element

var scope =  angular.element(document.getElementById('input-refcode')).scope();
var desiredValue = scope.account.referral_code_string;
Sign up to request clarification or add additional context in comments.

Comments

0

This might help you.

inject $timeout service in controller

$timeout(function(){
 console.log(document.getElementById('input-refcode')); },200);

Note: Use capital B in getElementById not getElementbyId.

Comments

0

var app = angular.module('todoApp', []);
app.controller('TodoListController', function($scope){

        $scope.$watch('referral_code_string'), function(){ console.log($scope.referral_code_string)};
    
     console.log(document.getElementById('input-refcode').value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="todoApp" ng-controller="TodoListController as todoList">
<input class="input referral-code" disabled="true"  ng-model="referral_code_string"  ng-init="referral_code_string = 'abc'" id="input-refcode" value="abc">
</div>

Comments

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.