Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't get the value of a input within a datalist?

I have a datalist box that looks like this:

<td>
    <input list="screens.screenid-datalist" type="text" id="screens.screenid" onblur="validate('0','0','jacques')">
    <datalist id="screens.screenid-datalist">
        <option value="Login"></option>
        <option value="ScreenCreator"></option>
    </datalist>
    <label id="val-screens.screenid" class="Label_Error" style="visibility: hidden;">*</label>
</td>

and I have a JavaScript code which has to get the value from this datalist.

I tried all the following things to get the value

document.getElementById('screens.screenid').value
document.getElementById('screens.screenid').text
document.getElementById('screens.screenid').innerHTML
document.getElementById('screens.screenid').option

and it just does not seem to work.

Is there something wrong with my JavaScript or with my HTML code?

when I use the console to get the value: when I use the console to get the value

like image 502
Jacques Koekemoer Avatar asked Aug 15 '13 09:08

Jacques Koekemoer


People also ask

How can you connect a Datalist to an input tag?

To bind the <datalist> element to the control, we give it a unique identifier in the id attribute, and then add the list attribute to the <input> element with the same identifier as value. Only certain types of <input> support this behavior, and it can also vary from browser to browser.

What is the input type for Datalist in HTML?

The datalist tag is introduced in HTML5. The <datalist> tag should be used with an <input< element that contains a "list" attribute. The value of "list" attribute is linked with the datalist id.

What is alternative for Datalist?

Alternatives to <datalist> If you can actually limit the user's input to the predefined options, and you don't need free-entry, you can simply use the standard <select> element. If you need type-entry autocomplete, the most widely supported solution is Javascript.


1 Answers

Took sometime to figure this out. Look at my code below :

function validate(){
    console.log(document.getElementById('screens.screenid').value); //WORKS

    console.log(document.getElementById('screens.screenid').text);
    console.log(document.getElementById('screens.screenid').innerHTML);
    console.log(document.getElementById('screens.screenid').option);

}
<input list="screens.screenid-datalist" type="text" id="screens.screenid" onblur="validate('0','0','jacques')">
   <datalist id="screens.screenid-datalist">
	  <option value="Login"></option>
	  <option value="ScreenCreator"></option>
  </datalist>
<label id="val-screens.screenid" class="Label_Error" style="visibility: hidden;">*</label>
<a href='#' onclick="validate">validate</a>

Now the first one will get the value as expected, but the text option will not work for the obvious reason that you do not have text here. Also, innerHTML will get you the child html and not the value. Further more , you can't get innerHTML of an input list, but you can get it for the datalist.

Try this : console.log(document.getElementById('screens.screenid-datalist').innerHTML);

I tried it and got the innerHTML without any hassle :

<option value="Login"></option>
<option value="ScreenCreator"></option>

Find the bin here : http://jsbin.com/inigaj/1/edit

like image 193
The Dark Knight Avatar answered Oct 20 '22 01:10

The Dark Knight