0

I use a 3rd party shopping module for this site and hence i cannot tinker with the sourcecode of this module.

Everthing works fine but here is my issue. On the checkout option the summary contains field which are redundant and i want to hide this fields.

Discount:  GBP £0.00
Sub total: GBP £90.00
Shipping:  GBP £10.00
Handling:  GBP £0.00
Total:     GBP £100.00

As you see above only 3 fields have values. I want to use Javascript and hide the fields which do not have any values like "Discount", "Shipping" and "Handling".

Here is a Fiddle to my code

Here is my code

<div class="CartTotalAmountContainer">
    <div class=" TotalSalesOrderDetailDiscountAmount">
        <div>
            <label> <span>Discount:</span> 
            </label>
        </div>  <span>GBP £0.00</span>

    </div>
    <div class="SubTotalAmount">
        <div>
            <label> <span>Sub total:</span> 
            </label>
        </div>  <span>GBP £90.00</span>

    </div>
    <div class="TotalShippingAmount">
        <div>
            <label> <span>Shipping:</span> 
            </label>
        </div>  <span>GBP £10.00</span>

    </div>
    <div class="TotalHandlingAmount">
        <div>
            <label> <span>Handling:</span> 
            </label>
        </div>  <span>GBP £0.00</span>

    </div>
    <div class="TotalAmount">
        <div class="dnnLabel">
            <label> <span>Total:</span> 
            </label>
        </div>  <span>GBP £100.00</span>

    </div>
</div>

No my logic is i can access the topcontainer of the elements by using

var X= document.getElementsByClassName("CartTotalAmountContainer");

but how do i access the data inside the individual spans and make the style="display:none" for their parent divs.

6
  • 1
    you can't put a class on this fields and hide with CSS? Commented Mar 18, 2014 at 13:37
  • 1
    No. The HTML is generaes dynamically. It has to hide the divs conditionally. Only if the amount is 0.00 or else display the div Commented Mar 18, 2014 at 13:38
  • If you create the list dynamically, in your loop, you can add a class if there is no value. Commented Mar 18, 2014 at 13:38
  • it is a bit easier to select elements and their children in JQuery but if you have to use vanilla javascript I think you'll have to use nodes Commented Mar 18, 2014 at 13:43
  • is Opencart? WooCommerce? Magento? Commented Mar 18, 2014 at 13:48

2 Answers 2

3

Try .querySelector:

var x = document.querySelectorAll(".CartTotalAmountContainer > div > span");

for(var i=0; i<x.length; i++) {
    if(x[i].innerHTML == "GBP £0.00") {
        x[i].style.display = "none";
    }
}

Demo!

And here's some reading for ya: https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll

Sign up to request clarification or add additional context in comments.

1 Comment

WOnderful... I just changed the line inside the loop to this " x[i].parentNode.style.display = "none";" and it worked as i wanted. Thanks a lot
1

Here is a complete native JS solution which works in IE8 and below: jsFiddle: http://jsfiddle.net/giri_jeedigunta/46QyE/ var outerDiv = document.getElementsByTagName('div'), cartContainer, i, j, cartInnerContent; // Since getElementsByclassName doesnt work in IE8: for(i = 0; i < outerDiv.length; i++) { if(outerDiv[i].className === 'CartTotalAmountContainer') { cartContainer = outerDiv[i]; cartInnerContent = cartContainer.getElementsByTagName('div'); break; } }

// Queryselector have limitations in IE8 and below
for(j = 0; j < cartInnerContent.length; j++) {
    if(typeof cartInnerContent[j].getElementsByTagName('span')[1] !== 'undefined') {
        var spanContent = cartInnerContent[j].getElementsByTagName('span')[1].innerHTML, 
            priceSplit = spanContent.split('£')[1].split('.')[0];
            console.log(priceSplit);

        if(parseInt(priceSplit) === 0) {
            cartInnerContent[j].style.display = 'none';
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.