0

I'm trying to add a class to a bootstrap modal when a button is clicked. Nothing fancy except that there is only one modal which is populated with JSON content. To make my script working properly I need top add some identifier to the modal so the script knows which modal is open.

The modal looks like this:

<div data-show="false" data-width="800" tabindex="-1" aria-hidden="false" role="dialog" class="modal fade in" id="shop-modal">
    <div class="modal-dialog modal-lg">
      ... content from JSON ...
      </div> 
    </div> 

The modal is opened like this:

<a data-vid="123456" data-handle="link-to-product.html" class="trigger-cart" href=""></a>

 $('.trigger-cart').click(function(e){
    e.preventDefault();
    var $this = $(this);
    var url = $this.data('handle') + '/?format=json';
    var vid = $this.data('vid');
    quick_shop(url, vid);
  });

The function which is triggered on button click is this:

function quick_shop(url, vid){

  $('#shop-modal').addClass(vid);

$.getJSON(url, function (data){
  $('#shop-modal').modal('show');
   // do stuff....

What i try to do is to add vid as a class to the opened modal. As I tried with this $('#shop-modal').addClass(vid); I also tried stuff like this:

$('#shop-modal').modal('show').addClass(vid);

This doesn't work. Does anyone know how to do that?

Meules
  • 1,349
  • 4
  • 24
  • 71
  • @JoshCrozier: Ok thx but `vid` is something like `123456`. Why doesn't addClass(vid) works then? This works though, `addClass(''+vid+'')` – Meules Sep 01 '14 at 16:52
  • @JoshCrozier: I don't want to add vid as class but 123456 – Meules Sep 01 '14 at 16:54
  • You're right. I read *"I try to do is to add `vid` as a class"* literally. It's worth noting that CSS classes starting with numbers are invalid - http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-selectors – Josh Crozier Sep 01 '14 at 17:13

1 Answers1

1

Based on your comment:

Ok thx but vid is something like 123456. Why doesn't addClass(vid) works then? This works though, addClass(''+vid+'')

It sounds like you are passing a number to addClass but addClass requires a string. That is why addClass(''+123456+'') works but not addClass(123456), the use of empty strings on either side of the number type coerce it into being a string.

If instead you did addClass((vid).toString()) it would also work. Although, I have to also mention that using just a number as a class name is not valid.

Community
  • 1
  • 1
DerekK
  • 61
  • 3