i have here the code..
<div>
<input type="hidden" value="hello" />
</div>
<div>
<input type="hidden" value="world" />
</div>
is it possible to select the div with the value "hello" inside and change the selected div's color to red...?
$("div input[value='hello']").css("background","red"); //i have this in mind
//but i think its wrong:D
any help please..
Coffee script version of @pebbl above answer.
This does it:
Live example
Or if by "color" you really meant "background color":
Live example
Throwing this out there for informational purposes.
In practice I'd use @BoltClock's or @T.J. Crowder's solutions.
This uses the
has-selector
(docs) to select<div>
elements that have a<input value="hello">
as a direct descendant.The reason I'd prefer the others is because of the fairly simple valid CSS selectors they use. This is a valid alternative, but will likely perform a little slower.
You want to select the input, then take its parent
div
:Just as a future note to those that come across this question, these answers are correct for when searching for the specific
value
as an attribute i.e. the one hard-coded in the HTML — completely correct as per the question asked. However, if the value of the field is changed by the user at any point the value attribute is not updated, only the element's value property. This will lead to unexpected behaviour in the form of selecting elements that actually have different current values... instead — or at least until I find a better way — I've been using the following:The above creates a new jQuery pseudo selector, which can be used like so:
Which will select all inputs that start with the value "test", or:
Which will select all inputs that contains "test" anywhere in it's value.
Also supported are
!
not,$
ends with or=
equals...