How can i convert this statement to Jquery Completely
if(document.getElementById('txt1') != null){
$("#txt1").val(document.getElementById('txt1').value.toUpperCase());
}
How can i convert this statement to Jquery开发者_如何学C Completely
You can use a function with .val()
like this:
$("#txt1").val(function(i, val) { return val.toUpperCase(); });
...and it it doesn't find an id="txt1"
element it just won't run on any, that's the way jQuery chains work, so your if()
check is taken care of as well.
or like this
var $txt = $("#txt");
if($txt.length){
$txt.val($txt.val().toUpperCase());
}
精彩评论