1

My View code is as below where I am calling a javascript function on key up event. I want to pass the control value to javascript function. I tried using 'this' and @this, but it didn't work.

@Html.TextBoxFor(m => m.LeaveDetailsList[0].LeaveTaken, new {id = "numLeaveTaken1", width = "100", @onkeyup = "checkInp2(this);" })

** Edited ** Javascript code:

function checkInp2(ControlId) {
    var x = document.getElementById(ControlID).value;
    if (isNaN(x)) {
        document.getElementById("btnSave").disabled = true;
        return false;
    }
    else {
        document.getElementById("btnSave").disabled = false;
    }
}
4
  • Show the javascript function Commented May 9, 2015 at 9:28
  • @StephenMuecke Code Post edited. Code added. Commented May 9, 2015 at 9:30
  • So you don't actually want the id, you want the value of the control. Are you happy with jquery code? Commented May 9, 2015 at 9:32
  • @StephenMuecke Yes will do. Commented May 9, 2015 at 9:32

2 Answers 2

1

I figure your onkeyup event should work (even though inline events is not really top of the best practice list now days), but not with the current function. Try changing it to:

function checkInp2(ControlId) {
   var x = ControlId.value;
   ...
}

You're sending this, referring to the element that triggered the event, to the function so there is no need to use getElementById.

If you instead wanted to get the element explicitly by Id, you could use..

document.getElementById('numLeaveTaken1').value; //pure JavaScript 

$('#numLeaveTaken1').val(); //jQuery

In this case you can skip this all together.

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

2 Comments

Oh yes, I have to use ControlId.value instead of document.getElementById. Now it is working.
@RKh I'm glad! I'll note this here since I didn't mention it in my answer: this is referring to the element that triggered the event. So when you send this, in your example, you're actually sending a reference to the entire textbox. If you're already using jQuery, this code could be a lot neater though :)
1

You should avoid polluting your mark-up with behavior and instead use Unobtrusive Javascript. Give the textboxes a class name

@Html.TextBoxFor(m => m.LeaveDetailsList[0].LeaveTaken, new { @class = "leave-taken" })

Then the script

var button = $('#btnSave'); // cache it
$('.leave-taken').keyup(function() {
  if(isNaN($(this).val())) {
    button.prop('disabled', true);
  } else {
    button.prop('disabled', false);
  }
});

or it could be reduced to

$('.leave-taken').keyup(function() {
    button.prop('disabled', !isNaN($(this).val()));
});

Note also the controls with can now be style with css - .leave-taken { width: 100px; }

1 Comment

I have to mention CSS classes also. In that case Can we give two classes as: @class = "leave-taken, Yellow" ?

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.