Checkbox creation using clone() method of jquery - javascript

I my form I have duplicate checkbox like
<input type="checkbox" name="todayDimensionStones[].isIssued" id="isIssued" value="Yes"/>
I am creating another checkbox using above using clone() method using jquery.The checkbox box is created successfully.But when I checked and submit the form containing the newly created checkbox and retrieve the value of newly created checkbox,it seems to be empty ie ''. What I to do solve this problem.If any have an idea ,please share with me

You have to change the attribute name
or you have to add [] at the end of the name.
If you send two inputs with the same name without [] at he end, php gives you the last one only.

If anyone stumbles about this: value attribute is probably missing. Set it manually after cloning:
On the cloned element with some checkboxes do:
$clone.find(':checkbox').each(function() {
$(this).attr('checked','checked')
});
Thats on IE9. See https://bugs.jquery.com/ticket/10550

Related

How to store input type checkbox into json object using either Javascript or jQuery?

I wanted to store all my clicked input accordions(<input type="checkbox">) into json object, I have tried it but it is storing/taking the last input accordion clicked event only, it is not storing/taking all the input clicked accordions that is whatever I click on any accordion(of <input type="checkbox">) that should store into json object using either Javascript or jQuery ?
I am not sure where I am doing wrong ? Please help me regarding this ? Thanks in advance. Sample fiddle.
Because you are doing submitButtonValue[name] = $(this).val(); and the name you are getting it from $(this).attr('name'); on click of checkbox which is same for all the checkboxes. So everytime it replaces the value since object will have only one name contained. I would suggest you to either use different names for each checkbox or find some other way to uniquely store your checkbox values.
A Demo here

Get input Array Laravel not working

I'm creating an array of input in my form. And i wanna get this array on my controller in Laravel.
My inputs is hidden type, and i add this fields dynamically through JavaScript, using Backbone.
I add this inputs on my form:
<input type="hidden" name="id_poi[]" class="id_poi" value="">
The value is also set dynamically in JavaScript.
After i add this input, this is my DOM:
So, i can add many inputs on my form.
When i'll handle this array on Laravel, i just get the value of the last input of this array.
I'm testing in this way:
public function store() {
var_dump(Input::all());
return;
}
And i had this output:
What i'm doing wrong to fail to get the values in an array?
EDIT:
I had other element with the same name in my form, and i change it.
But the problem isn't solved yet, when i submit the form, the elements added dynamically isn't sent to PHP. Resulting in the same output above.
inputs is hidden name edit. Example
name="id_poi[0]"
name="id_poi[1]"
Sory, my English not good.

How to pass value between parent window and child window

I am using Jsp, Structs. From a parent window (e.g)parent.jsp, I invokes child.jsp, which has a list of checkbox values. I select more than one values, then put it in a arraylist and send back to parent.jsp and store it in a textarea.
Now If i want to select some more values from child.jsp. When i again invokes child.jsp, the checkboxes i already checked will be checked there and i dont want the empty unchecked boxes.
Could someone help me in this.......If u send some sample code means its very useful.
Thanks in advance.....
Aadhira.
As I understand, when you go to the child.jsp for the second time you need to preserve the checked checkbox values. The only solution i see for this is pass these checked box parameters to the child.jsp form.

get radio box value in jquery

Am I under the wrong impression that jquery or JS can retrieve the values of radio buttons in a form? The reason i ask is because in my code the script i use to check for all fields in a form, does not seem to recognise the value in id="contact2" in the form, which is a radio group. I have posted my code at jsfiddle.net and would appreciate some feedback as to how I can correct this. Many thanks
Fiddle: http://jsfiddle.net/xGrb9/
You need to do it like this:
$('input:radio[name=bar]:checked').val();
Because of how radio buttons are checked/unchecked and their values are stored. (from the jQuery docs). You also need to make sure that the radio buttons have different IDs, which is a classic gotcha.
Finally, when testing if a radio button has not been selected at all, make sure to test against undefined and not an empty string for compatibility across browsers.
EDIT: looked at your code, and you need to do two things:
1. Change IDs of the buttons, to something like "contact2a" and "contact2b" so they are unique.
2. Change your var customer2 = line to var contact2=$("input[name=contact2]:checked").val();
Change this line:
var contact2=$("#contact2").val();
to:
var contact2=$('input[name="contact2"]:checked').val();
You need the checked because otherwise it finds both inputs.
Also, technically, all IDs should be unique, ie, not used on 2 elements on the page.
You should be able to use .val()
See this: http://api.jquery.com/val/
Both of your radio buttons have the same ID. That doesn't work in HTML. You'll have to refer to the buttons separately and select the one that is checked.
You can simply call
$("[name=contact2]:checked").val()

Getting HTML with the data using jQuery

I have a form which has many elements (e.g. textarea, input, select), after users have entered some data the states of these elements should change.
For example, an input[type="radio"] element will have the attribute checked="checked" if a user has checked it. The value attribute of an input[type="text"] element will contain the text entered by user.
The problem is that the html string returned by $('#form1').html() does not contain these data.
Feel free to take a look at this example:
http://jsfiddle.net/cmNmu/
You can see that no matter what your inputs are, the html returned is still the same (having no attribute data).
Is there any easy way to collect the html including their states?
Thanks in advance.
use below code getting the value of input type text via jQuery
alert($("input:text").val())
Maybe you could use the 'onblur' event handler to set the value of the element when you leave it
You should get the value using :
$('#form1').find(':input').val();
$('#form1').find(':radio[name=gender]:checked').val();
if you have multiple input then you can filter them bu their name or class or even id. Then you will need to select input using .find(':input[name=input_field_name]'). My Suggestion is : use name property instead of other property if you want to use form.
People usually use $('#form1').serialize() to get the values. If html() doesn't return both the source and data, I don't think that there is something you can other than manually constructing the full html by looking at the data.
Live demo: http://jsfiddle.net/cmNmu/6/
By using the jQuery formhtml plugin written by gnarf:
jQuery html() in Firefox (uses .innerHTML) ignores DOM changes
The changes in the input elements can be reflected in the html string returned by formhtml().
Thank you very much everyone.

Categories