1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Opening a Modal window through a button Click

Discussion in 'HTML & Website Design' started by Orawolf, Nov 19, 2015.

  1. #1
    Hello Gurus.

    I am using HTML5 and I have following code that I am trying to use to open a modal window through a button click. But nothing is happening. I am a learner and developer. What am I missing ? Any Idea?

    Below is Java Script


    <script type="text/javascript">
        $(function() {
            var doOk = function() {
              $("#openModal").dialog("close");
           
            }
            var dialogOpts = {
              modal: true,
              buttons: {
                "Ok!": doOk
              },
              height: "400px",
              autoOpen: false
            };
            $("#openModal").dialog(dialogOpts);
            $("#openDialog").click(function() {
              $("#openModal").dialog("open");
            }); 
        });
      </script>
    
    Code (JavaScript):
    And below is HTMLCode

    <button class="btnsignin"  style="margin-left:60%;"type="submit" id="openDialog">
                        next</button>
                        <div id="openModal" class="modalDialog">
        <div>
            <a href="#close" title="Close" class="close">X</a>
            <h2>Modal Box</h2>
            <p>This is a sample modal box that can be created using the powers of CSS3.</p>
            <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
        </div>
    HTML:
    Thanks,
    Darsh
     
    Orawolf, Nov 19, 2015 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    hdewantara, Nov 20, 2015 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    No offense, but what the **** does this have to do with HTML 5 or CSS3?!? You're using the fat bloated train wreck of halfwit nonsense known as jQuery and the even dumber train wreck known as jQuery-UI to do the heavy lifting.... Doing this with CSS3 would involve no JavaScript, and probably be best handled by "abusing a checkbox" to do it using the new :checked attribute and adjacent sibling selectors to do it.

    <div class="dialogWrapper">
    	<input type="checkbox" id="modalBoxTrack" class="dialogCheckbox">
    	<label for="modalBoxTrack">Open Dialog</label>
    	<div class="dialogWindow">
    		<label for="modalBoxTrack" class="dialogClose>X</label>
    		<h2>Modal Box</h2>
    		<p>
    			This is a sample modal box that can be created using the powers of CSS3.
    		</p><p>
    			You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.
    		</p>
    	<!-- .dialogWindow --></div>
    <!-- .dialogWrapper --></div>
    Code (markup):
    Then you just...

    .dialogWindow { display:none; }
    .dialogCheckbox:checked+label+.dialogWindow { display:block; }
    Code (markup):
    THAT would be doing it using CSS3. Naturally you'd have to size or position the dialogWindow from the CSS as well, and visibly hide the checkbox.
     
    deathshadow, Nov 20, 2015 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    DS's hate for everything jquery is well-known here. And he's probably right about it. But if you still want to use JQ, do something like this:

    Example: http://jsfiddle.net/kumarmuthaliar/GG9Sa/1/

    
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <style type="text/css">
    .modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
    }
    .modalDialog:target {
    opacity:1;
    pointer-events: auto;
    }
    .modalDialog > div {
    width: 400px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
    }
    .close {
    background: #606061;
    color: #FFFFFF;
    line-height: 25px;
    position: absolute;
    right: -12px;
    text-align: center;
    top: -10px;
    width: 24px;
    text-decoration: none;
    font-weight: bold;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    border-radius: 12px;
    -moz-box-shadow: 1px 1px 3px #000;
    -webkit-box-shadow: 1px 1px 3px #000;
    box-shadow: 1px 1px 3px #000;
    }
    .close:hover {
    background: #00d9ff;
    }
    </style>
    <script type="text/javascript">//<![CDATA[
    $(window).load(function(){
    });//]]>
    </script>
    </head>
    <body>
    <a href="#openModal">Open Modal</a>
    <div id="openModal" class="modalDialog">
    <div> <a href="#close" title="Close" class="close">X</a>
    <h2>Modal Box</h2>
    <p>This is a sample modal box that can be created using the powers of CSS3.</p>
    <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
    </div>
    
    Code (markup):
     
    qwikad.com, Nov 20, 2015 IP
  5. Orawolf

    Orawolf Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Thank you everyone for providing various solution approach. However, all examples provided show launching of Modal window either through a checkbox or through a link and they work great. But my requirement is to launch the same modal window through a button click. How do I achieve that?

    I also tried following without any JS anywhere and it is still not working. I feel we are very close to solving it. Also tried removing # in front of div ID in the onclick event without any success.

        <button class="btnsignin"  style="margin-left:60%;"type="submit" id="openDialog" onclick="showModalDialog('#openModal')">
                        next</button>
                        <a href="#openModal">Open Modal</a>
                        <div id="openModal" class="modalDialog">
        <div>
            <a href="#close" title="Close" class="close">X</a>
            <h2>Modal Box</h2>
            <p>This is a sample modal box that can be created using the powers of CSS3.</p>
            <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
        </div>
    </div>
    
    Code (markup):
    Thank you much for your time.
    Darsh
     
    Last edited: Nov 21, 2015
    Orawolf, Nov 21, 2015 IP
  6. KewL

    KewL Well-Known Member

    Messages:
    245
    Likes Received:
    16
    Best Answers:
    3
    Trophy Points:
    128
    #6
    If you must...

    <button id="openModal">Open</button>
    
    <div id="modal">
      <button id="closeModal">Close</button>
      <h2>Testing 123</h2>
    </div>
    Code (markup):
    #modal {
      position: fixed;
      background: blue;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      opacity: 0;
      z-index: -1;
      transition: opacity 0.2s;
    }
    
    #modal.active {
      opacity: 1;
      z-index: 2;
    }
    Code (css):
    $('#openModal').click(function() {
      $('#modal').addClass('active')
    })
    
    $('#closeModal').click(function() {
      $('#modal').removeClass('active')
    })
    Code (javascript):
    Demo:
    http://codepen.io/anon/pen/QjPBMa
     
    Last edited: Nov 21, 2015
    KewL, Nov 21, 2015 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Notice how my suggestion used a label and said to HIDE the checkbox? Style the label to look like a button, end of problem.

    You seem to be thinking default appearance of the tag instead of just leveraging the working tags. Remember if you are choosing your tags based on what they look like, you're choosing the wrong tags for the wrong reasons.
     
    Last edited: Nov 22, 2015
    deathshadow, Nov 22, 2015 IP
  8. Orawolf

    Orawolf Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Thank you all. I ended up using Kewl's approach.
     
    Orawolf, Nov 30, 2015 IP