1

I have an input within a list that utilizes ng-repeat. The input's id is listed as "newLicName". Problem is, that since it's ng-repeat, that means that it creates multiple inputs (one for each item in the list), all with the same ID ("newLicName"), so when I try to retrieve that value in the javascript (through document.getElementById('newLicName')), it only retrieves the value of the first input with that ID. My solution to that issue was to remove the input and place it within a div outside of the li tag and then move it next to the specific list item when it is clicked, but I can't seem to find how to do it using angularJS

For example:

<li ng-repeat="item in items">
  {{item.name}}
  <a href="#" ng-click="doThis(item)">Click</a>
  <div id="placeWithin"></div>
</li>

<div id="content">
  <input id="newLicName" value="Content in here" />
</div>
$scope.doThis = function (item){
    //code here  
}

In this example, when I click the hyperlink, I want the div located outside of the li tag to be placed within the li tag. How would I achieve this through angularJS?

2
  • 1
    Try <input id="placeWithin_{{$index}}"></div> to assign different ids. Then you can call doThis(item, $index) to access it. Commented Jun 3, 2016 at 20:07
  • @HanletEscaño that did the trick, thank you! Commented Jun 3, 2016 at 20:18

1 Answer 1

1

As someone explained in the comments, you could simply do this to make unique inputs:

<input id="placeWithin_{{$index}}"></div>

However, if what you are trying to do is retrieve the values from the inputs, you don't have to resort to this javascript hack. You can bind your input with ng-model and follow this post to learn how to deal with ng-model inside an ng-repet:

Binding ng-model inside ng-repeat loop in AngularJS

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

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.