tags:

views:

411

answers:

3

I have the following input:

  <input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname"/>

How can I use jQuery to make this element a read-only input without changing the element or its value?

+6  A: 

simply add the following attribute

// for disabled i.e. cannot highlight value or change
disabled="disabled"

// for readonly i.e. can highlight value but not change
readonly="readonly"

jQuery to make the change to the element (substitute disabled for readonly in the following for setting readonly attribute).

$('#fieldName').attr("disabled","disabled")

or

$('#fieldName').attr("disabled", true)
Russ Cam
A: 

Maybe use atribute disabled:

<input disabled="disabled" id="fieldName" name="fieldName" type="text" class="text_box" />

Or just use label tag: ;)

<label>
Rin
`display: none;`?
Kobi
thx for notice I copied it from question source ... ;)
Rin
The question said "With jQuery" (which implies it should be done dynamically) and "read-only" (which is different to disabled).
David Dorward
A: 
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head >
    <title></title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;

</head>
<body>
    <div>
        <input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname" />
    </div>
</body>

<script type="text/javascript">
    $(function()
    {
        $('#fieldName').attr('disabled', 'disabled');

    });
</script>
</html>
Raghav Khunger