I have two divs
<div class="tab-pane active" id="home">
<div class="tab-pane" id="profile">
Each of this div has forms in it which has a hidden field <input type="hidden" name="selectedRadioValue" id="selectedRadioValue">
So I would like to create a javascript which would assign a value to the hidden input field.
if($("#home").hasClass("active")) //Dont know whether this is
// true
{
assign "radio1" as the value in the input hidden field //what could be the equvalent
// statement for this?
}
else if($("#profile").hasClass("active"))
{
assign "radio2" as the value in the input hidden field //what could be the equvalent
// statement for this?
}
Im sorry for this kind of a question.Im new to javascript,so I have tried a little bit of statements myself,but I need help.How to accomplish this?Please help
Solution:
$(function () {
$('button').on('click', function () {
if ($("#home").hasClass("active")) {
$('.selectedRadioValue').val('radio1');
} else if ($("#profile").hasClass("active")) {
$('.selectedRadioValue').val('radio2');
}
});
});
You need to loop through every tab pane that is active, then select form that is in that tab pane and then get hidden value in that form. Then set value that you get to radio button in that same form.
Also, it's not good to have multiple elements with same id like #selectedRadioValue
. Consider using classes instead.
$(function() {
// Loop through all active tab panes
$('.tab-pane.active').each(function() {
// Get hidden input value
var hiddenValue = $('form #selectedRadioValue', $(this)).val();
// Set radio input value to hidden input value
// Radio inputs should have '.radio-input' class tho
$('form .radio-input', $(this)).val(hiddenValue);
});
});
Ohh, you need to assign hidden inputs to radio1/radio2? Then your code is correct and all you need to do is assign radio1/radio2 as the value of your input. Just add class like radio-hidden
to it and then do $('.radio-hidden').val('radio1');
etc.
$(function () {
$('button').on('click', function () {
if ($("#home").hasClass("active")) {
$('.radio-hidden').val('radio1');
} else if ($("#profile").hasClass("active")) {
$('.radio-hidden').val('radio2');
}
});
});
Here is fiddle: http://jsfiddle.net/3NAC9/
if($("#home").hasClass("active"))
{
$("#selectedRadioValue").val("radio1");
}
else if($("#profile").hasClass("active"))
{
$("#selectedRadioValue").val("radio2");
}
Should do what you want I think?
Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...
Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...
Update: A Solution below! I'm fairly new to website development but I've been tasked with developing e2e (end-to-end) tests for a developing website that uses AngularJS. As a result I've been looking ...
Update: A Solution below! I'm fairly new to website development but I've been tasked with developing e2e (end-to-end) tests for a developing website that uses AngularJS. As a result I've been looking ...
Apologies in advanced, but I'm a complete noob at Polymer. So far I've done an HTML file (treated as external dialog box), with some stuff in it. First off, the user clicks a button from the main ...
Apologies in advanced, but I'm a complete noob at Polymer. So far I've done an HTML file (treated as external dialog box), with some stuff in it. First off, the user clicks a button from the main ...
I'm familiar with the way call(), which you can pass a variable number of arguments that will be loaded into a function's parameters when called. I'm trying to do something related where I recurse ...
I'm familiar with the way call(), which you can pass a variable number of arguments that will be loaded into a function's parameters when called. I'm trying to do something related where I recurse ...