开发者

jQuery selectors with more than 1 criteria

开发者 https://www.devze.com 2023-03-23 06:27 出处:网络
trying to do something pretty obvious really but its kinda not making any sense / not workin开发者_开发技巧g for me ...

trying to do something pretty obvious really but its kinda not making any sense / not workin开发者_开发技巧g for me ...

The idea is simple, when the document is loaded $(document).ready() get all input elements that have attribute type="text" on them and add the css class "textbox" ...

<script type="text/javascript">
    $(document).ready(function () {
        var textboxes = $(":input [type = 'text']");
        textboxes.each().addClass("textbox");
    });
</script>

Any ideas why this doesn't work ?? ...

EDIT:

Wow .. still have a lot to learn about jquery-isms ... starting to love jquery though :) it's simplicity really does the trick.


$(document).ready(function () {
    $("input[type='text']").addClass("textbox");
});

No need for the colon before the element input.

$("input[type='text']") will reference all elements so no need for an each()


The primary issue in your code is that you have a space between the :input pseudo-selector and the type attr selector. Removing the space works.

That said, .each() is meant to receive a function which should run on each element in the matched collection. It then returns the collection for further chaining. In your example, the given each was useless so I removed it. Further adjustments to make the swlector match properly but efficiently should give you want you want:

$( function()
{
    $( 'input[type="text"]' ).addClass( 'textbox' );
} );


Maybe I'm missing something, but shouldn't this work?

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

Is there some reason you are first evaluating to a var?

0

精彩评论

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