Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<h1>Pay your bill</h1>
<div>
  <div class="group-label">
    <strong>Select an amount</strong>
  </div>
  <div>
    <div>
      <label for="alpha">
        <input type="radio" name="options" id="alpha" value="full">
        Pay in full: $386.00
      </label>      
    </div>
    <div>
      <label for="bravo">
        <input type="radio" name="options" id="bravo" value="other">
        <input type="text" placeholder="Other Amount" id="other-amount" />
      </label>      
    </div>
  </div>
</div>

<button class="submit" disabled>
  Pay my bill
</button>


<h2>Why this is a problem</h2>

<p>This is what most developers will do. You must define good UX for people using a keyboard or screen reader.</p>

<p>When the "other amount" field is focused, it also selects the radio button. This creates a keyboard trap, meaning someone using a keyboard cannot advance.</p>

<p>The second radio button will have no discernable name.</p>

<p>Placeholder text cannot be used as a name for an input field.</p>

<p>It is invalid HTML to put an interactive control or input field inside a label</p>


              
            
!

CSS

              
                *:focus {
  outline-offset: 4px;
}

html {
  background-color: #eee;
  font-family: arial;
}
body {
  max-width: 420px;
  margin: 0px auto;
}

.group-label {
  margin-bottom: .5rem;
}

input[type=radio] {
  width: 1.5rem;
  height: 1.5rem;
  cursor: pointer;
}

input[type=text] {
  font-size: 1rem;
  line-height: 1.5rem;
}

label {
  display: grid;
  grid-template-columns: 3rem 1fr;
  padding: 1rem;
  align-items:  center;
  margin-bottom: .25rem;
  background: #fff;
  line-height: 2rem;
  border-radius: .5rem;
  cursor: pointer;

}

button {
  padding: 1rem 2rem;
  font-size: 1rem;
  color: #fff;
  line-height: 2rem;
  font-weight: bold;
  margin-top: 1rem;
  background: #2978F6;
  border: none;
  border-radius: 3rem;
  transition: .2s;
  cursor: pointer;
  
  &:active {
    background: #1F5EC1;
  }
  
  &:disabled {
    background: #999;
  }
}
              
            
!

JS

              
                function checkOtherAmount() {
  if ($("input#other-amount").is(":placeholder-shown")) {
    // The placeholder is displayed
    // alert("The placeholder is displayed");
    $(".submit").attr('disabled', 'disabled');
  } else {
    // The placeholder is not displayed
    $(".submit").removeAttr('disabled');
  }
}

// If other amount field is focused/tapped, change radio choice
$('#other-amount').focus(function() {
  $('#bravo').prop('checked', true);
  checkOtherAmount();
});

// On starting to type, enable the submit button
$('input#other-amount').keypress(function() {
  checkOtherAmount();
});

$( "input#other-amount" ).on( "blur", function() {
  checkOtherAmount(); 
});


$('input[name="options"]').on('change', function() {
  checkOtherAmount();
  var $payment =  $('input[name="options"]:checked').val();
  
  if($payment == "other" ) {
    checkOtherAmount();
    $('#other-amount').focus(); // Focus the input field
  } else {
    $(".submit").removeAttr('disabled')
  }  
});

$(".submit").click(function() {
  alert("Great success");
});

              
            
!
999px

Console