开发者

Prototype - Element with styleclass in element with an id?

First of all: I'm new to Prototype JS Framework! Until now I worked with jQuery. In jQuery I am able to get an element by coding:

$('#myitemid .myitemclass').val()

html:

<div id="myitemid">
    <input type="text" class="notmyclass" />
    <input type="开发者_JS百科text" class="myitemclass" />
    <input type="text" class="notmyclass" />
</div>

But how to do this in prototype? I tried to code:

$('myitemid .myitemclass').value

but this won't work. Can U help me plz?


Use $$ which returns all elements in the document that match the provided CSS selectors.

var elemValue = $$('#myitemid input.myitemclass')[0].getValue();

Also input.myitemclass is better than .myitemclass because it restricts search to input elements with class name .myitemclass.


If you want to get the named element myitemid, simply use $('myitemid'). This is equivalent to $('#myitemid') or document.getElementById('myitemid'). Your case is more complex, since you want to select a child of a named element. In that case you want to first find the named element, then use a selector on it's children.

$('myitemid').select('input.myitemclass')

Then, to access it's value (since it's a form element), you can add .getValue().

$('myitemid').select('input.myitemclass').getValue()


Should be faster

$("myitemid").down("input[class~=myitemclass]").value
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜