1

I have a "Change" button that makes a form active or locked when clicked. Though the button only works when I click "change", and then doesn't :/

<div class="holder">
  <div class="question active">Q1<input id="one" type="text">
<div class="button passive"><input type="button" value="Change" id="changeone"></div></div></div>

$('input#changeone').click(function() {
    if( $('#one').attr('readonly', true) ) {

      $("#changeone").attr('value', 'Save');
      $('div.button').parent().css('background-color', 'red');
      $('#one').attr('readonly', false);

    } else {

      $("#changeone").attr('value', 'Change');
      $('div.button').parent().css('background-color', 'green');
      $('#one').attr('readonly', true);

    }

})

THANKS :)

**corrected indentation

THANK YOU ALL! You all responded so quickly and I thank everyone for their time :)

6 Answers 6

5

You are setting readonly to true in your if, rather than getting it. You need to use .attr(attributeName) to get the value.

$('input#changeone').click(function() {
    if( $('#one').attr('readonly') == 'readonly') {
        ...
    } else {
        ...
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

So should it look like this( $('#one').attr(readonly) = true ) { ?
It depends, the correct readonly attribute should be set as readonly="readonly" in the markup, so a positive test is attr('readonly') == 'readonly'. However as @Mrchief has pointed out there could be a standalone attribute (not valid XHTML). If you are in control of setting the readonly attribute correctly, then the comparison above will do just fine.
5

Need to check for true like this:

if( $('#one').attr('readonly') == true) )

You can also just have readonly attribute by itself

<input readonly>

In that case, use this:

$('#one').is('[readonly="readonly"]') || $('#one').is('[readonly="true"]')

Combining the two into single statement:

var isReadonly = $('#one').attr("readonly");
if(isReadonly && isReadonly.toLowerCase()!=='false') { 
   // this is readonly
}

Comments

2

Try this

$('input#changeone').click(function() {
    if( $('#one').attr('readonly')) {

    $("#changeone").attr('value', 'Save');
    $('div.button').parent().css('background-color', 'red');
    $('#one').attr('readonly', false);

} else {

    $("#changeone").attr('value', 'Change');
    $('div.button').parent().css('background-color', 'green');
    $('#one').attr('readonly', true);

}

})

Comments

2

try this one

<div class="holder">
  <div class="question active">Q1<input id="one" type="text">
<div class="button passive"><input type="button" value="Change" id="changeone"></div></div></div>

$('input#changeone').click(function() {
    if( $('#one').attr('readonly') == true ) {

    $("#changeone").attr('value', 'Save');
    $('div.button').parent().css('background-color', 'red');
    $('#one').attr('readonly', false);

} else {

    $("#changeone").attr('value', 'Change');
    $('div.button').parent().css('background-color', 'green');
    $('#one').attr('readonly', true);

}

})

Comments

2

$('#one').attr('readonly', true) sets the readonly attribute to true. You need to read it and compare it with true

if ($('#one').attr('readonly') == true) // ...

Comments

2

should be

$('input#changeone').click(function() { if( $('#one').attr('readonly') ) {

  $("#changeone").attr('value', 'Save');
  $('div.button').parent().css('background-color', 'red');
  $('#one').attr('readonly', false);

} else {

  $("#changeone").attr('value', 'Change');
  $('div.button').parent().css('background-color', 'green');
  $('#one').attr('readonly', true);

}

})

instead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.