javascript - Jquery use data-id and disable and clear input field - Stack Overflow

For a variety of reasons I want to change the code below to look out for data attributes rather than a

For a variety of reasons I want to change the code below to look out for data attributes rather than a class called "mygroup".

HTML

<select id="apply">
    <option value="email">Email</option>
    <option value="website">Website</option>
</select>
<input type="text" class="input" id="email" placeholder="[email protected]" data-id="application"/>
<input type="text" class="input" id="website" placeholder="www.example" data-id="application"/>
<textarea class="input" rows="4" cols="50" placeholder="text*" data-id="applicationdetails"></textarea>

JS

// Apply Fields
function applyField() {
    $(document).on('change', '#apply',function() {
        var selected = $(this).find(':selected').val(),
            elem = $("#"+selected);
        $(".mygroup").addClass('hidden');
        elem.removeClass('hidden');
    });
    $("#apply").trigger('change');
};

FIDDLE

How do I change the code above apply to data-id of "application" rather than using the class "mygroup". For bonus points can anyone tell me how to clear the content of the field that is being hidden and also disable it?

For a variety of reasons I want to change the code below to look out for data attributes rather than a class called "mygroup".

HTML

<select id="apply">
    <option value="email">Email</option>
    <option value="website">Website</option>
</select>
<input type="text" class="input" id="email" placeholder="[email protected]" data-id="application"/>
<input type="text" class="input" id="website" placeholder="www.example." data-id="application"/>
<textarea class="input" rows="4" cols="50" placeholder="text*" data-id="applicationdetails"></textarea>

JS

// Apply Fields
function applyField() {
    $(document).on('change', '#apply',function() {
        var selected = $(this).find(':selected').val(),
            elem = $("#"+selected);
        $(".mygroup").addClass('hidden');
        elem.removeClass('hidden');
    });
    $("#apply").trigger('change');
};

FIDDLE

How do I change the code above apply to data-id of "application" rather than using the class "mygroup". For bonus points can anyone tell me how to clear the content of the field that is being hidden and also disable it?

Share Improve this question edited Jul 11, 2013 at 9:54 Thirumalai murugan 5,9248 gold badges34 silver badges54 bronze badges asked Jul 11, 2013 at 9:51 J.ZilJ.Zil 2,4498 gold badges46 silver badges82 bronze badges 1
  • possible duplicate of how to select data-id and data-action in jQuery – revolver Commented Jul 11, 2013 at 9:56
Add a ment  | 

2 Answers 2

Reset to default 4

You simply refer to it as $('[data-id="application"]').

As for the clearing and disabling, use .prop() and .val(''):

$('[data-id="application"]').addClass('hidden').prop('disabled', true).val('');
elem.removeClass('hidden').prop('disabled', false);

DEMO

Try to use the attribute selector, ie all elements with attribute data-id instead of having class myClass

function applyField() {
    $(document).on('change', '#apply',function() {
        var selected = $(this).find(':selected').val(),
            elem = $("#"+selected);
        $("input[data-id]").addClass('hidden');
        elem.removeClass('hidden');
    });
    $("#apply").trigger('change');
};

jQuery(function($){
    applyField();
});

Demo: Fiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251466a4618701.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信