How to keep jquery from descending too far?

Posted by Earlz on Stack Overflow See other posts from Stack Overflow or by Earlz
Published on 2010-06-11T23:51:34Z Indexed on 2010/06/12 0:03 UTC
Read the original article Hit count: 146

Filed under:
|

Hello, I am having a problem with jquery going a bit too far on pattern matching of CSS classes and IDs.

I have some markup that looks like this:

  <div id="blah">
    <div class="level2">
      <input type="text" />
    </div>
    <div class="levelA">
      <div class="level2">
        <input type="text" value="foo"/>
      </div>
    </div>
  </div>
  <input type="text" value="bar" />

I want for the 3 inputs to say

  • Hello
  • Foo
  • Bar

so I have this line of jquery:

$('#blah .level2 input').val('hello');

the problem now is that jquery is a bit too liberal in it's pattern matches and matches both the first and second.

How can I prevent this kind of thing from happening?

A live example is at http://jsbin.com/opelo3/4

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html