Categories
discuss

Using JavaScript to manipulate HTML input (checkbox) elements via type instead of name

I am implementing an HTML form with some checkbox input elements, and I want to have a Select All or DeSelect All button. However, I do not want to rely on the name of the input element (like this example) but rather the type because I have multiple checkbox groups with different names. Is there a way to check and uncheck all checkbox input elements within a form with JavaScript by relying on the type instead of the name?

Edit: We rely on YUI libraries, so I have access YUI if that provides a solution.

Answer

This should do it:

<script>
function checkUncheck(form, setTo) {
    var c = document.getElementById(form).getElementsByTagName('input');
    for (var i = 0; i < c.length; i++) {
        if (c[i].type == 'checkbox') {
            c[i].checked = setTo;
        }
    }
}
</script>

<form id='myForm'>
<input type='checkbox' name='test' value='1'><br>
<input type='checkbox' name='test' value='1'><br>
<input type='checkbox' name='test' value='1'><br>
<input type='checkbox' name='test' value='1'><br>
<input type='checkbox' name='test' value='1'><br>
<input type='button' onclick="checkUncheck('myForm', true);" value='Check'>
<input type='button' onclick="checkUncheck('myForm', false);" value='Uncheck'>
</form>
Categories
discuss

What is the best way to pass information from java to c++?

I have a java application I need to pass some info to a C++ program. It has been suggested that I use some simple socket programming to do this. Is this the best way? If not what are the alternatives? If so, how should I go about learning about socket programming?

Answer

You have a few options:

  • Pass a file from Java to C++. This is probably simplest. It’s easy to test and shouldn’t require any 3rd party libraries on either end.
  • Use sockets as mentioned. In C++, if you require a cross-platform solution a library such as ACE or boost will save you some heartache
  • Use JNI to call from Java to C++ or vice versa. This is probably the most difficult, but most performant.

For learning sockets, a Google search for “java socket tutorial” or “c++ socket tutorial” will give you lots of information.

Categories
discuss

Initializing disposable resources outside or inside try/finally

I have seen two ways of acquiring and disposing resources. Either:

Resource resource = getResource();
try { /* do something with resource */ }
finally { resource.close(); }

or:

Resource resource = null;
try { resource = getResource(); /* do something with resource */ }
finally { if (resource != null) resource.close(); }

I was wondering which style is preferable. The first one avoids the if condition, while the second one (I presume) handles the case of thread abort right after the assignment but before entering the try block. What other pros and cons do these styles have over each other? Which one should I preferably use?

Answer

In C#, just use the using statement:

using (Resource resource = GetResource())
{
    /* Do something */
}

This is the idiomatic way of cleaning up resources, and relies on the resource in question implementing the IDisposable interface. (Java now has a similar try-with-resources statement, for resources implementing AutoCloseable.)

There’s no risk of a thread abort in Java happening between the assignment and entering the try block – aborts only occur during sleeps and waits. EDIT: I can’t actually find this in the spec, which is somewhat worrying. Hmm.

Categories
discuss

Thread safety of static blocks in Java

Let’s say I have some Java code:

public class SomeClass {
    static {
        private final double PI = 3.14;
        private final double SOME_CONSTANT = 5.76;
        private final double SOME_OTHER_CONSTANT = 756.33;
    }

  //rest of class
}

If a thread is initializing SomeClass’s Class object and is in the middle of initializing the values in the static block when a second thread wants to load SomeClass’s Class again, what happens to the static block? Does the second thread ignore it assuming it’s already initialized even though the first thread is not done? Or does something else happen?

Answer

If the first thread hasn’t finished initializing SomeClass, the second thread will block.

This is detailed in the Java Language Specification in section 12.4.2.

Categories
discuss

Difference between break and continue statement

Can anyone tell me the difference between break and continue statements?

Answer

break leaves a loop, continue jumps to the next iteration.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..