Can I add the required attribute to an input field using css?

Go To StackoverFlow.com

1

I have a recaptcha widget in my form and i want it to be made mandatory. since i dont have any direct control over the widget in my html, i was wondering if i can add the required attribute to it after it has been rendered.

ie. if i add the folloing css

#recaptcha_response_field{background-color:#000000;}

it does color up the recaptcha widget text field. in the same vein, if there was some way of setting the required="required" attribute for input fields via css, i'd be able to make it mandatory.

2012-04-05 19:11
by Rishav Sharan


3

Does the following work for you:

$(function() {
    $("#recaptcha_response_field").attr('required','required');
});
2012-04-05 19:17
by mreyeros
unfortunately, no. i added it to my js file but it had no effect - Rishav Sharan 2012-04-05 19:48
got it to work after removing the function tags.

$("#recaptcharesponsefield").attr('required','required');

thanks - Rishav Sharan 2012-04-05 19:52

glad I could hel - mreyeros 2012-04-06 12:58


1

If I understand you correctly, this should do the trick:

$(function() {
    $("#recaptcha_response_field").css({background-color:#000000;});
});

Without jQuery:

document.onload = function() {
    document.getElementById("recaptcha_response_field").style['background-color'] = '#000000';
};

I'm not 100% sure on the second one, but I can't test it right now.

2012-04-05 19:15
by Elliot Bonneville
i think you misunderstood me. i wanted to put the attribute "required" on the field. I gave an example where i can put in the style on the field. I just wanted to know if it was possible to put in the required attribute in a similar way - Rishav Sharan 2012-04-05 19:50
Ads