Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Css class to all <input type'text'> elements? Javascript / Css?

Tags:

html

jquery

css

I want to apply a CSS class to every textbox in my site:

        <div class="editor-label">
            <label for="FoodType">FoodType</label>
        </div>
        <div class="editor-field">
            <input id="HelpText" name="FoodType" type="text" value="" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>

And I thought, Hey! Easy. I'll add a jquery function to find them all in the masterpage.

<script type="text/javascript">
    $(document).ready(function(){
        $('input').addClass('textbox');
    }
</script>

Unfortunately this will also select the submit button. How can i only select input elements that have a text type attribute?

Alternativly is this possible using entirely CSS?

If both these methods are not possible, i guess i will just have to manually add the class to every textbox i make?

like image 340
4imble Avatar asked May 14 '10 16:05

4imble


People also ask

How do you call a CSS class in JavaScript?

className = "MyClass"; In the css class MyClass you can set the background image.. You can create another css class with different background image and set it according to the conditions.. Show activity on this post.

How do I add a class dynamically?

You can use Javascript for adding a class: setAttribute and className both method are used to set class (class attribute), these method are not used to adding another class with existing one.


1 Answers

AFAIK it's:

$('input[type=text]').addClass('textbox');

And you can do similar in the CSS file, but that requires higher version of CSS / depending how broad you want to be with older browsers.

See attribute selectors in here. Note that:

"Browser support: The only browser that doesn’t support CSS3 attribute selectors is IE6. Both IE7 and IE8, Opera and Webkit- and Gecko-based browsers do. So using them in your style sheet is definitely safe."

like image 163
eglasius Avatar answered Sep 24 '22 21:09

eglasius