Get span tag name

0

I need to get the generated name inside the id="getname" and put it inside the id="name", I made the code below, but it did not work.

The span tag="getname" generates the client's name, in the case of Fulano da Silva and then with the script it would return only the first name.

<span data-customer="name" id="pegarnome" ></span>
<span id="nome"></span>

<script>

    var str = document.getElementById("pegarnome").innerHTML;
    var res = str.split(" ",1);
    document.getElementById("nome").innerHTML = res;

</script>
    
asked by anonymous 30.06.2017 / 22:30

1 answer

1
<script>    
    var str = document.getElementById("pegarnome").innerHTML;
    var arr = str.split(" ");
    var res = arr[0];
    document.getElementById("nome").innerHTML = res;
</script>

Split returns an array of String, you can use it by passing to the element the first index of the array, which would be the first name.

    
30.06.2017 / 22:49