tags:

views:

28

answers:

1

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

+5  A: 

You want

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

> means direct descendant.

Thomas
Ah I had seen that character before but being unable to search for it, didn't know what it meant. That does exactly what I need(in my actual code). Thanks!
Earlz
http://www.w3.org/TR/CSS2/selector.html came up after a quick server for "css operators" (I wanted to see what other operators existed).
halkeye
@halk that's a good amount of information there. I had no idea there was a `+` operator
Earlz