Change input value with attribute in javascript

javascript set input value by name
input value javascript
jquery set value of input field
how to set default value in textbox in html using javascript
how to get value of textbox in javascript
how to get value of textbox in javascript from html
how to get value of textbox in javascript function
setattribute javascript

i have data-right-answer which keep the right answer, if the answers right i should change border color, if not i should write the right answer in input value, i can not imagine how i should to do it with attribute.

var btn = document.getElementById("myBtn");
btn.addEventListener("click", checkInputs);

var inputs = document.querySelectorAll("input");

function checkInputs(){
	for( var i = 0; i < inputs.length; i++ ){
		if( inputs[i].getAttribute("data-right-answer") == inputs[i].value ){
			inputs[i].style.border = "1px solid green";
		} else{
			// inputs[i].value = getAttribute("data-right-answer");
		}
	}
}
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<button id="myBtn">Start</button>

You have forgot to get the attribute value in the else condition.

inputs[i].value = inputs[i].getAttribute("data-right-answer");

var btn = document.getElementById("myBtn");
btn.addEventListener("click", checkInputs);

var inputs = document.querySelectorAll("input");

function checkInputs(){
	for( var i = 0; i < inputs.length; i++ ){
		if( inputs[i].getAttribute("data-right-answer") == inputs[i].value ){
			inputs[i].style.border = "1px solid green";
		} else{
			inputs[i].value = inputs[i].getAttribute("data-right-answer");
		}
	}
}
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<button id="myBtn">Start</button>

HTML DOM Input Text value Property, Add the class attribute with the value of "democlass" to a <h1> element: document. Before setting the attribute: Change an input field to an input button:. Sometimes we need to set a default value of the <input> element, This example explains methods to do so. This property set/return the value of value attribute of a text field. The value property contains the default value, the value a user types or a value set by a script. text: It specifies the value of input text field.

inputs[i].dataset.rightAnswer => this will capture the right answer from the "data-" attribute. :D

var btn = document.getElementById("myBtn");
btn.addEventListener("click", checkInputs);

var inputs = document.querySelectorAll("input");

function checkInputs(){
	for( var i = 0; i < inputs.length; i++ ){
		if( inputs[i].dataset.rightAnswer == inputs[i].value ){
			inputs[i].style.border = "1px solid green";
		} else{
      inputs[i].value = inputs[i].dataset.rightAnswer;
		}
	}
}
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<button id="myBtn">Start</button>

DOM Element setAttribute() Method, <input type="text" id="example"> // Setup text field <script Or if you want to avoid JavaScript entirely: You can define it just using HTML way to get the placeholder is through the place holder attribute getElementById("example"); // Get text field elem.value = "My default value"; // Change field </script>. Javascript can be used to change the attribute(s) of a HTML element, such as a paragraph, a header, an image, or a list, or any various HTML elements. For example, by changing the src attribute of an img tag, we can change the image entirely to something else with Javascript.

$('#myBtn').click(function(){
$('input').each(function(){
	if($(this).attr('data-right-answer')==$(this).val())
	{
	$(this).css('border','1px solid green')
	}
else{
	if($(this).val()!='')
	{
			$(this).val($(this).attr('data-right-answer'))
	}
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<input type="text" data-right-answer="Lolik">
<button id="myBtn">Start</button>

Set the value of an input field, In JavaScript you can change elements by using the following syntax: Here, the srcattribute of an image is changed so when the script is called, it changes the input element from the 'myButton' id myButton.value = "I'm a changed button";  I want a very simply Javascript function (which I can't get to work, not so simple for me). When Someone inserts a number in an inputfield, the value of an other inputfield should change to that va

JavaScript/Changing elements, The first time the textarea element is given focus, the property “counter” for the textarea element Setting the value of the text input element through JavaScript. What I want to do with this form is that when you hit submit/button to first change the value of a field and then submit the form as usual. Change value of input

How to set the value of a form element using Javascript, Instead of removing the attribute or setting its value to be null , it instead sets the attribute's value to the string "null" . If you wish to remove an  If the specified attribute already exists, only the value is set/changed. Note: Although it is possible to add the style attribute with a value to an element with this method, it is recommended that you use properties of the Style object instead for inline styling, because this will not overwrite other CSS properties

Element.setAttribute(), In addition to the attributes that operate on all <input> elements regardless of their type, Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value property. > Above code is working fine on FF but on IE7 name attribute is not set at all. Just because the 'NAME' attribute is not shown in the alert doesn't mean that it is not set. Try to submit the form and take a look at the submitted query string i.e. the URL, it still submits the newly created form fields.