HELP href="javascript not working inside javascript file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jayvi
    New Member
    • Dec 2009
    • 5

    HELP href="javascript not working inside javascript file

    I used this code inside of a javascript file to switch out an 'add to cart' button:

    Code:
    var addDisplay = document.getElementById('cart-add');
    	addDisplay.innerHTML = "<a class='item_add' href='javascript:;'><img src='images/shop/cart-add.jpg' width='118' height='26' border='0'></a>";
    the original 'add to cart' button that is in the htm file works, but when it is switched out with the one above, it does not work! what am I doing wrong? it's the same code!
    Last edited by Dormilich; Dec 6 '09, 12:40 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it's the same code!
    hard to tell without the original code, but this link is certainly incapable of doing anything (useful).

    Comment

    • jayvi
      New Member
      • Dec 2009
      • 5

      #3
      Thanks for replying... here is the original code in the htm file:

      <div id="cart-add"><a class="item_add " href="javascrip t:;"><img src="images/shop/cart-add.jpg" width="118" height="26" border="0"></a>
      </div>

      the link adds an item to the cart just fine, but when it is switched out with the one in the javascript file, it no longer works... is this the code you were talking about? if you think you can help, please PM me and I'll send you links to the originals... thanks!!!!!!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        the link adds an item to the cart just fine
        are you sure? I can’t see anything (there's not even an obvious javascript function) that would do something.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Code:
          <a href='/' onclick='simpleCart.add(); return false;'>
          I’d say, this makes the difference (compared to the previous code).

          Comment

          • jayvi
            New Member
            • Dec 2009
            • 5

            #6
            Thanks Dormilich,

            but that's actually the last thing I tried, and it still won't work =[

            Any other ideas?

            Comment

            • jayvi
              New Member
              • Dec 2009
              • 5

              #7
              Oh, okay, I used that code for the rest of the javascript... for the switch out, and it makes the switching work a lot better, without taking me to the top... thanks!!

              But I still need help with adding the other items to the cart... I input the same code into the javascript file, and it switches everything out great, but then the add to cart button doesn't work anymore... PLEASE!! ANYONE?? Is my request clear? Or do I need to give more details? Please ask me if so, thanks!!! =]

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Make the href point to a real URL which adds the item to the cart, so if JavaScript is not enabled, users can still add items to the cart.

                Also, just change the onclick: you don't need to replace the whole container element each time.

                Comment

                • jayvi
                  New Member
                  • Dec 2009
                  • 5

                  #9
                  Thanks acoder,

                  I would like to do that with the link in case of js being disabled, but I'm not sure how yet. I can barely get the link changing to work =\

                  How do you just change the onclick:?

                  I think the problem is that the js file only "displays" the other elements needed for the add to cart to work, but doesn't actually place it in the htm file, which is what I think needs to be done...

                  What solution would you use? PHP maybe? I'd rather not rebuild, but if I have to... I'm hoping that switching out only the onclick would work.

                  THANKS!!

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    To add to the cart without JavaScript, you would need server-side code, e.g. PHP.

                    As for the onclick, I'm assuming here that the simpleCart.add( ) call works as expected. Thinking about it again, you don't need to do anything to the Add to Cart button. Leave it be, because it's the same for all items.

                    Just for reference: To change the onclick, reset it like this:
                    Code:
                    elem.onclick = function() { simpleCart.add(); return false; }

                    Comment

                    Working...