In many cases when you have a search field it can be useful to provide a list of suggestions to the user as they type. This small interaction provides several benefits to the user: It affirms to the user that the site search is working It provides potential searches an alternatives to the user before they even complete a search … Read More
I was working on an admin area for a site that had checkboxes next to headings and subheadings. This script allows for a parent checkbox to be checked when you select one of its children.
1 2 3 4 5 |
$('#chkbox-cont input[type="checkbox"]').change(function() { if (this.checked) { $(this).parents('ul').parents("ul li").find("input[type=checkbox]:first").prop("checked", true); } })</code>; |
To automatically open tiers on load if a checkbox is checked use
1 2 3 4 5 |
$('#chkbox-cont input[type="checkbox"]').each(function(){ if (this.checked) { $(this).parents("ul").css('display', 'block'); } }); |
You should avoid writing HTML in Javascript code because it can be difficult to modify and maintain. For example, this can get messy, unwieldy and very easy to accidentally cause syntax errors:
1 |
var htmlStr = "<div><p class='my-class'>Lorem ipsum</p><p class='summary'>Dolor sit amet</p></div>"; |
Instead, consider using a template. HTML template Write your HTML template in a <script> tag with the type set to text/template. This prevents the browser from rendering or attempting to … Read More