[Solved] Javascript: How can this code be reduced or improved?

Using a single function that is called with the elements as parameters var directorOneText = document.getElementById(‘directorOneText’); var directorTwoText = document.getElementById(‘directorTwoText’); function changeText(t1, t2) { t1.style.display = (t1.style.display === “block”) ? “none” : “block”; t2.style.display = “none”; } var directorOneClickEvent = document.getElementById(‘directorOne’).addEventListener(“click”, function(){ changeText(directorOneText, directorTwoText)}); var directorTwoClickEvent = document.getElementById(‘directorTwo’).addEventListener(“click”, function(){ changeText(directorTwoText, directorOneText)}); #directorOne {background:#333; padding: 20px;} … Read more

[Solved] Create a function to send mail from a div

You’re on the right track: $( document ).ready( function() { $(‘.icon-send-mail’).on(‘click’, function(e) { var mailto_link = ‘mailto:’ + $(this).attr(‘id’); var win = window.open(mailto_link, ’emailWindow’); }); }); 2 solved Create a function to send mail from a div

[Solved] How do I show a div with a form only if a button is clicked?

You can use the onClick event to do that: Working Example HTML: <button id=”some_id”>Hide div</button> <form id=”some_form”> <form> javascript: <script type=”text/javascript”> var theButton = document.getElementById(‘some_id’); theButton.onclick = function() { document.getElementById(‘some_form’).style.visibility=’hidden’; } </script> 2 solved How do I show a div with a form only if a button is clicked?

[Solved] Math.ceil not working with negative floats

The Math.ceil method does actually round up even for negative values. The value -12 is the closest integer value that is at higher than -12.369754. What you are looking for is to round away from zero: value = value >= 0 ? Math.ceil(value) : Math.floor(value); Edit: To use that with different number of decimal points: … Read more

[Solved] JavaScript : Find (and Replace) text that is NOT in a specific HTML element?

If, and only if, there are no nested <span>-tags, you can search for /(<span\b[^>]*>[\s\S]*?<\/span>)|(\b(?:foo|bar)(?:\s+(?:foo|bar))*)/g and replace it with the function function matchEvaluator(_, span, word) { if (span) return span; return ‘<span class=”widget”>’ + word + ‘</span>’; } the part (<span\b[^>]*>[\s\S]*?<\/span>) searches for the span element. That’s the part, where no nested <span>-element is allowed. The … Read more

[Solved] add to values separated by a comma [closed]

You changed it to an array. You can add to an array in javascript with .push(); var totalAmount = [55.99, 7.01]; totalAmount.push(5); console.log(totalAmount); // produces something like [55.99, 7.01, 5] [edit] Yeah, the whole format of the question threw me off. If you’re wanting the SUM of an array of numbers, you can do so … Read more

[Solved] Div width resize based on how many radio buttons are selected [closed]

jsFiddle: http://jsfiddle.net/K8TAS/90/ <input type=”checkbox” value=”1″ checked disabled/> <input type=”checkbox” value=”2″/> <input type=”checkbox” value=”3″/> <br/><br/> <div style=”height:200px;width:100px;float:left;background:yellow;” class=”resizeDiv” id=”div0″>Default</div> <div style=”height:200px;width:100px;float:left;background:red;” class=”resizeDiv” id=”div1″>1</div> <div style=”height:200px;width:100px;float:left;background:blue;display:none;” class=”resizeDiv” id=”div2″>2</div> <div style=”height:200px;width:100px;float:left;background:green;display:none;” class=”resizeDiv” id=”div3″>3</div> <script> $(“input[type=checkbox]”).click( function() { var amount = 2; $(“input[type=checkbox]”).each( function() { if ( $(this).val() != “1” ) { if ( $(this).is(“:checked”) ) { $( “#div” … Read more

[Solved] i need to create an event listener. but i dont want to use any framework

Here’s how to create an EventListener without JQuery… [].forEach.call(document.querySelectorAll(‘.element’), function (el) { el.addEventListener(‘click’, function() { // Your code }, false); }); Documentation: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener This article is also interesting: http://blog.romanliutikov.com/post/63383858003/how-to-forget-about-jquery-and-start-using-native 2 solved i need to create an event listener. but i dont want to use any framework

[Solved] Understanding For-in Loop

This is where good indentation is key. var test = { “key1”: val1, “key2”: [ { “a”:1, “b”:[{},{}] }, { “a”:1, “b”:[{},{}] } ] }; Here you can clearly see that it in fact has 2 items. 0 solved Understanding For-in Loop