0

is there any way in angular 2 to pick name/id of input inside divs?

html:

<div class="form-pages" (click)='function()'>//if it's possible then what should I put into function param?
    <fieldset>
     <input type="text" name="firstName"/>
     <input type="text" name="firstName"/>
</fieldset>
</div>

component: function(str){this.var=str}

0

2 Answers 2

1

Not sure if template variables of child elements can be accessed

<div class="form-pages" (click)='function(input1.value, input2.value)'>//if it's possible then what should I put into function param?
    <fieldset>
     <input #input1 type="text" name="firstName"/>
     <input #input2 type="text" name="firstName"/>
</fieldset>
</div>

If this doesn't work you can use

<div class="form-pages" (click)='function()'>//if it's possible then what should I put into function param?
    <fieldset>
     <input #myinput type="text" name="firstName"/>
     <input #myinput type="text" name="firstName"/>
</fieldset>
</div>
@ViewChildren('input') inputs:QueryList<ElementRef>;

myFunction() {
  let inputs = this.inputs.toArray();
  console.log(inputs[0].value;
  console.log(inputs[1].value;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ok, I did it alone, thanks for previous answer, but for me it works well, It's needed to call $event.target.name method which will return name of clicked target.

<div (click)="setFocus($event.target.name)">
<input type='text' name='input1 />
<input type='text' name='input2 />
</div>

in componentsetFocus(str){this.var = str)

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.