How do I get the IDs and VALUE of an input text array?

0

I am making a screen that adds images to the database and after doing so, ready them by adding a text box in which I will capture their DESCRIPTION, example:

<form>
    <input id="1" name="ftp_desc"  maxlength="256" placeholder="Añade una descripción" required="required" type="text">
    <input id="2" name="ftp_desc"  maxlength="256" placeholder="Añade una descripción" required="required" type="text">
    <input id="3" name="ftp_desc"  maxlength="256" placeholder="Añade una descripción" required="required" type="text">
</form>

<a type="button" onclick="javascript: agregarPasoDieciseis()">Finalizar</a>

Javascript

function agregarPasoDieciseis() {

    var descripcion = document.getElementsByName("ftp_desc").value;        
    alert(descripcion);
}

As I get the IDs and the VALUE of the input with Javascript, the previous code did not work for me.

    
asked by Ricardo Sauceda 06.11.2017 в 18:17
source

1 answer

2

getElementsByName returns a set of objects with the name ftp_des , you have to access them through a cycle as if it were an array and thus obtain its id and value attributes.

var docs = document.getElementsByName('ftp_desc');

for (var i = 0; i < docs.length; i++){
	console.log('id: ' + docs[i].id + ',' + 'value: ' + docs[i].value);
}
<input id="1" name="ftp_desc"  maxlength="256" placeholder="Añade una descripción" required="required" type="text">
<input id="2" name="ftp_desc"  maxlength="256" placeholder="Añade una descripción" required="required" type="text">
<input id="3" name="ftp_desc"  maxlength="256" placeholder="Añade una descripción" required="required" type="text">
    
answered by 06.11.2017 / 18:23
source