<div class="oneItem" id="product_1">
<div class="imageItem"></div>
<div class="infoItem">
<p>Цена: <span class="priceItem">44.34</span>руб.</p>
<span>
Кол-во:
<span class="rateItem" onclick="rateChange( 1, 1 );">◄</span>
<input class="rateInput" type="text" value="1" />
<input class="priceForOne" type="hidden" value="44.34" />
<span class="rateItem" onclick="rateChange( 1, 0 );">►</span>
</span>
<input type="submit" value="Добавить в корзину" />
</div>
</div>
<div class="oneItem" id="product_2">
<div class="imageItem"></div>
<div class="infoItem">
<p>Цена: <span class="priceItem">68.30</span>руб.</p>
<span>
Кол-во:
<span class="rateItem" onclick="rateChange( 2, 1 );">◄</span>
<input class="rateInput" type="text" value="2" />
<input class="priceForOne" type="hidden" value="34.15" />
<span class="rateItem" onclick="rateChange( 2, 0 );">►</span>
</span>
<input type="submit" value="Добавить в корзину" />
</div>
</div>
<script type="text/javascript">
function getElemsByClass( className, context ) {
var nodeList = [];
context = context || document;
if ( context.getElementsByClassName ) {
nodeList = Array.prototype.slice.call(
context.getElementsByClassName( className )
);
} else {
var elems = context.getElementsByTagName("*"),
classMatch = new RegExp('(?:^| +)' + className + '(?: +|$)');
for( var elem, i = 0 ; elem = elems[ i ]; i++ ) {
if ( classMatch.test( elem.className ) ) {
nodeList.push( elem );
}
}
}
return nodeList;
}
function rateChange( product_id, type ) {
var product = document.getElementById( 'product_' + product_id );
rateInput = getElemsByClass( 'rateInput', product ),
priceForOne = getElemsByClass( 'priceForOne', product ),
priceItem = getElemsByClass( 'priceItem', product ),
count = parseInt( rateInput[ 0 ].value ),
price = parseFloat( priceForOne[ 0 ].value );
count = type == 0 ? ++count : --count;
rateInput[ 0 ].value = count;
priceItem[ 0 ].innerHTML = ( price * count ).toFixed( 2 );
}
</script>