Reputation: 135
So I am using a hidden form field and populating it with data from my JQuery code onSubmit. The problem is, the JQuery populates BOTH Type="Hidden" fields. I only want it to populate the name="moment" one, How can I fix this?
Here is my html:
<form method="post" action="momentactions.php">
<input type="hidden" name="album_id" value="<?php echo $album_id; ?>">
<input type="hidden" name="moment">
<input type="submit" name="submit" value="submit">
</form>
Here is my JQuery:
$('form').on('submit', function() {
values = [];
$('#container-top img').each(function() {
values.push($(this).attr('value'));
});
$('input[type=hidden]').val(JSON.stringify(values));
});
Upvotes: 1
Views: 134
Reputation: 55750
Try this
$('input[type=hidden][name=moment]').val(JSON.stringify(values));
Also do not forget to close the input tags..
$('input[name=moment]')
$('input[name=moment]:hidden')
are all valid selectors..
Upvotes: 2
Reputation: 141
This line of code
$('input[type=hidden]')
says "Give me all input elements with a type of hidden" since you only want one of them you can do a few different things.
You can give your desired input field an ID and then use the jQuery ID selector (#):
$('#idgoeshere')
or you can give your desired input field a class and use the class selector (.):
$('.classgoeshere')
or you can keep your current html and use an attribute selector which is just like your current selector only a different attribute:
$('input[name="moment"]')
Upvotes: 3
Reputation: 5381
I'd suggest you add id="moment" on your input tags
<input type="hidden" name="moment" />
change it to
<input type="hidden" name="moment" id="moment" />
so on your codes, you can just use
$('input#moment').val(JSON.stringify(values));
Upvotes: 0
Reputation: 254954
As simple as:
input[type="hidden"][name="moment"]
but still - it's not a good idea to extract all the values, serialize them, and send serialized
Upvotes: 3