jQuery ignoring the bars /

Asked

Viewed 55 times

1

I have the following code from button:

<button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('View all items in your shopping cart')) ?>" class="button btn-inline-ver-carrinho" onclick="setLocation('<?php echo $this->getUrl('checkout/cart') ?>')"><span><span>Ver Carrinho</span></span></button>

At the event onclick of this button, he returns me a url complete, but when I use this code through jQuery using a .append() as follows:

$j('#mini-cart').append("<button type='button' title='<?php echo Mage::helper('core')->quoteEscape($this->__('View all items in your shopping cart')) ?>' class='button btn-inline-ver-carrinho' onclick='setLocation('<?php echo $this->getUrl("checkout/cart") ?>')'><span><span>Ver Carrinho</span></span></button>");

The same code returns to me on onclick to url, but without the bars(/) and running this code normally without being through jQuery, the url is shown correctly.

Through the php, he returns me the following: onclick="setLocation("localhost/checkout/cart")"

Through the jQuery, he returns me the following:

onclick="setLocation("localhost  checkout  cart')'
  • assigning <?php echo $this->getUrl("checkout/cart") ?> to a variable, try to see if the value is correct. show the value in your question, I also do not know the setLocation function it is native? if you don’t post her code too so we can simulate.

  • @Caiqueromero Doing this and giving one console.log() in this variable, the value comes correctly.

  • Edit your question and provide the value that comes and the setLocation() function, so we can test and try to figure out the problem.

  • @Caiqueromero I edited the question, according to what you said.

1 answer

1


Explanation

Apparently your problem is separation into variables to avoid prolemes with multiple double-quoted strings.

That’s why I recommend using jQuery’s dynamic element creation and setting its attributes in hand, as well as below:

Code

function setLocation(href){
  document.location.href = href;
}

var btn = $('<button>'),
    spn = $('<span>'),
    str = 'View all items in your shopping cart',
    cls = 'button btn-inline-ver-carrinho',
    evt = "setLocation('<?php echo $this->getUrl(\"checkout/cart\") ?>')";
    
$(btn).attr({
  title: str,
  class: cls,
  onclick: evt
});

$(spn).html('<span>Ver Carrinho</span>');
$(btn).append(spn);


$('#mini-cart').append(btn);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id=mini-cart >
</div>

Remarks

I don’t have your php so replace where I put fixed by the php code you have that generates what you need.

  • 1

    That was the problem, the question of multiple single and double quotes. Separating this into equal variables you explained to me, it worked. Thank you so much for all your help and attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.