Joofe Kopp
Joofe Kopp

Reputation: 123

Getting value of an Input in an list

I am trying to get the value of an input field in a html list. I don't want the value which I set as the page loaded, i want the changed value (typed something in like abcd).

I tried it with innerHTML, innerText, textContent but its all the theme the old value. The only thing that works ist getting the value with the id from input, but that does not work for me and my project.

May anyone can help me.

Here is the list code:

<li id="testlist">
    <img onclick="functionxyz()" src="" height="40px" ondblclick="" width="50px"></img>
    <input id="id1" class="textlist" type="text" name="" value="old value"></input>
    <select id="id2" class="selectshortlist" name=""></select>
    <textarea id="" class="textfield1" cols="1" maxlength="1000" rows="1" name=""></textarea>
    <div class="ui-state-default sortable-number"></div>
    <input type="button" class="removelist"></input>
</li>

And that's the JavaScript code:

function()
{
    var sort1 = document.getElementById("testlist");
    alert(sort1);
    var liste = sort1;
    alert(liste.value);
    alert(liste.innerHTML);
    var savelist = new Array();
    for (var i = 0; i < liste.length; i++)
    {
        alert(liste[i].value);
        console.log(liste[i].value)
        //savelist.push(liste[i].textContent);
    }
}

Upvotes: 0

Views: 106

Answers (2)

Will
Will

Reputation: 4155

You can do it without jquery pretty easily if you're going that route.

function getInputVal() {
    var input = document.getElementsByClassName("textlist")[0];
    console.log(input.value);
}

FIDDLE

Upvotes: 0

Milimetric
Milimetric

Reputation: 13549

You tagged this with jquery, so the answer is, use jquery :)

var valueThatYouSeek = $('#testlist input.textlist').val();
console.log(valueThatYouSeek);
alert(valueThatYouSeek);

Upvotes: 1

Related Questions