0

I've been trying some experimentation with jQuery, basically it's not working, this is my HTML:

<input class="span4" value="" id="dpd1" type="text"> <!-- Input that needs value -->
<input class="span4" value="" id="dpd2" type="text" disabled> <!-- Input which needs 'enabled' -->

and here's what I'm trying with jquery:

$('#dpd2').click( function () {
    var value = $('#dpd1').val();
    if( value.length > 0 ){
    $("#dpd2").prop('disabled', disabled); }
    });

probably shouldn't be trying to have all this happen on click, but I haven't really done anything like this in jQuery before, so not sure what way is best for this type of situation.

Any help is appreciated.

EDIT: I picked the answer which best highlighted the shortest and effective way for me to achieve the outcome I wanted. But there are some other great answers there too! Thanks!!

3 Answers 3

3

You may want to do it in the blur of first textbox,

$('#dpd1').blur(function () {
    $("#dpd2").prop('disabled', !$.trim($('#dpd1').val()).length);
});

Demo

prop takes a boolean. And when you set your textbox to disabled initially you can no longer click on it. hence use blur to perform the action once you focus out of the first textbox.Use $.trim() to remove the whitespaces from ends of the textbox value

Sign up to request clarification or add additional context in comments.

3 Comments

Very nice, I like the size of this. Very informative. If I'm right you are using the !$('#dpd1').val().length to bring a true or false outcome?
Yes length if 0 means, !0 is true or else false
Thanks! You've just opened up a new chapter for me. Something I had never noticed due to naivety.
3

Use boolean values to disable / enable properties with prop() :

$('#dpd1').on('keyup', function () {
    $('#dpd2').prop('disabled', this.value.length < 1); 
});

FIDDLE

8 Comments

the one you just deleted... :rolleyes:
@Shomz that is why i deleted
@Shomz - Strangely enough Ankit commented on my error before he/she fixed the same error in his/her own answer.
Yeah, that's why I complained... typical lame behaviour. I like your final answer (although it's similar to mine), btw. +1
@Shomz yes i commented on after editing mine but deleted the answer because i was confused to choose how to show the desired effect whether attach an event to #dp1 or do something else.So it is not a lame behavior
|
1

Yeah, click won't work here, you need to implement change handler, then check whether the value is an empty string, and if not, change the disabled property on the other input.

Something like:

$('#dpd1').change(function () {
    $("#dpd2").prop('disabled', $('#dpd1').val() == '');
});

4 Comments

+1 for change - I really need to take my head out of PHP once in a while
Yeah, change is exactly what's needed here. Like I said, keyup and blur work as well, but change covers every scenario.
I used @PSL's answer but modified it with change as it seems to work better in my situation
@Ricki sure, whatever works best for the situation. Just don't forget the other options in case you need them in the future.

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.