开发者

how to pass id dynamically in jquery focus event?

开发者 https://www.devze.com 2023-03-14 10:27 出处:网络
Can I pass the button ID dynamically? $(\'#Button1\').focus(function () { if (document.getElementById(\'HiddenVal\').value != null && document.getElementById(\'HiddenVal\').value != \'\') {

Can I pass the button ID dynamically?

$('#Button1').focus(function () {
        if (document.getElementById('HiddenVal').value != null && document.getElementById('HiddenVal').value != '') {
            $('#txtbox1').val(document.getElementById('HiddenVal').value);
            $('#Button1', window.parent.document).css("background-color", "#fcc63b");
            document.getElementById('HiddenVal').value   = '';
        }
开发者_开发问答    });

Here I want to pass the button ID dynamically instead of using $('#Button1').

Can I use that?


yes you can- place your code inside a function...and pass id as a parameter to that function - like this..

function dynamicId(id){ 

        newId = '#'+id
        $(newId ).focus(function () { 

        ...
        ...
        });
}


Seems like your code could be written as :

    $('#Button1').focus(function () {
        var hiddenValue = $('#HiddenVal').val();
        if (hiddenValue  != null && hiddenValue != '') {
            $('#txtbox1').val($(hiddenValue );
            $(this).css("background-color", "#fcc63b");
            $('#HiddenVal').val('');
        }
    });

Regards,

Max

0

精彩评论

暂无评论...
验证码 换一张
取 消