0

I am using localStorage where in I am able to store in previous page and retrive it in this page. (Checked it using alert).

name12=localStorage.getItem("content");

Now my requirement is to display it into the input field and make it non-editable.

Please help me out with it. I have tried different things but I am not able to get it right.

Used onblur="localStorage.setItem(this.name, this.value) in the input tag

and also tried to use

if name_2.value = name12; in script tag 
Pedro Lobito
  • 75,541
  • 25
  • 200
  • 222
Sagar Chilukuri
  • 1,096
  • 2
  • 12
  • 26

3 Answers3

1

To make a field uneditable, you can use the html attribute disabled on the input field. http://www.w3schools.com/tags/att_input_disabled.asp

To set a default value for a field, you can use the html attribute value. In your case since the value is dynamic, you probably want do not want to do that inline in the html. One possible solution is to set the value attribute through javascript like below.

<script type="text/javascript">
    var name2 = document.getElementById("name_2");
    name2.value = localStorage.getItem("content");;
</script>

Set the value of an input field

Community
  • 1
  • 1
corkin
  • 91
  • 4
0

There are many places to find this information. Try reading the jquery documentation when you get stuck. For example, here is a page that describes how to set the value of an input element. StackOverflow also has many questions about this topic. A quick google search brings up this question about setting the value of input elements. We can also find SO questions about disabling input elements easily, like this one.

I encourage that you attempt to use more resources to find what you need before bringing your questions here.

Based on the answers to the questions I linked, we can set the input and then disable it to keep the value from changing:

$('#input').val(name12);
$('#input').prop('disabled', true);
Community
  • 1
  • 1
ShadowCat7
  • 779
  • 7
  • 16
  • Do you think the OP question deserves answer? you shouldn't help users that don't write proper question. This isn't good for SO. – Pedro Lobito Apr 21 '15 at 19:57
  • 1
    You're right. I'll edit to point the OP to documentation and other SO questions to encourage their use. – ShadowCat7 Apr 21 '15 at 20:00
0

You have to assign the value to input using javascript.

<input username" id="name_2" type="text" placeholder="name" name="name_2" required="required" autofocus="autofocus">
<script>
    var n = "5"; // read from storage here
    var inpt = document.getElementById("name_2");
    inpt.value = n;
    inpt.disabled = true;
</script>
wonderbell
  • 1,046
  • 8
  • 19