Steph L
Steph L

Reputation: 11

Popmodal Function Passing Multiple IDs

The following code works great. But it only calls modal1 to show when calling the popModal(). How do I change the code below where id == 'modal1' can be 'modal2', 'modal3' or 'modal4'. If I change the id value it works but I don't want it to be static - it could be any id value. Thoughts? Thanks for your assistance in advance.

var id= getUrlParameter('id');

if (id == 'modal1') {
     popModal();
}

function popModal()
{
    $('#modal1').modal('show');
}


function getUrlParameter(sParam)
{

    var sPageURL = window.location.search.substring(1);    

    // var to open modal: url http://example.com?id=modal1



    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}

Below is the js that I call to see the popup:

$(document).ready(function()
{ var id= getUrlParameter('id'); 
if (id == 'modal1') { popModal(); } })

I tried the following but it popups the same modal (modal1):

if (id == 'modal1' || id == 'modal2') { popModal();} function popModal() { $('#modal1' || '#modal2').modal('show'); }

And also added the following:

$(document).ready(function() { var id= getUrlParameter('id'); if (id == 'modal1' || id == 'modal2'){ popModal(); } })

Can't seem to get it to work - any help is appreciated.

Upvotes: 0

Views: 237

Answers (1)

dm4web
dm4web

Reputation: 4652

//### 1
if (id == 'modal1' || id == 'modal2' || id == 'modal3') {
     popModal(id);
}

//### 2
function popModal(id)
{
    $('#'+id).modal('show'); //### 3
}

TEST

  1. first checks whether the id is appropriate
  2. then call the function to show modal window with appropriate id
  3. to display the appropriate modal, id string must begin with #

Upvotes: 1

Related Questions