JavaScript javascript to get the value of a div if it has a class javascript class value change,javascript classlist value,javascript classname value,

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');
        }
    });
});
Answer:1

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);
    });
});

Edit

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/

Answer:2
if($("#home").hasClass("active"))                              
{
  $("#selectedRadioValue").val("radio1");
}
else if($("#profile").hasClass("active"))
{
   $("#selectedRadioValue").val("radio2");
}

Should do what you want I think?

Answer:3

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 ...

  1. react js without npm
  2. install react without npm
  3. use react without npm
  4. create react app without npm
  5. run react app without npm start

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 ...

  1. create click event javascript
  2. create click event js
  3. create click event in jquery
  4. create click event
  5. create click event c#
  6. create click event angular
  7. create click event vb.net
  8. create click event on div
  9. create click event in html
  10. create click event object
  11. create click event in java
  12. create click event programmatically
  13. create click event using javascript
  14. create click event using jquery
  15. create mouse click event javascript
  16. create button click event c#
  17. react create click event
  18. create button click event in android
  19. javascript create click event object
  20. jquery create click event dynamically

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 ...

  1. call function with string python
  2. call function with arguments javascript
  3. call function with arguments bash
  4. call function with variable name python
  5. call function with parameters powershell
  6. call function with parameters javascript
  7. call function with callback javascript
  8. call function with arguments python
  9. call function with array c++
  10. call function with variable powershell
  11. call function with variable javascript
  12. call function with multiple outputs python
  13. call function with kwargs
  14. call function with parameters python
  15. call function with self python
  16. call function with multiple outputs matlab
  17. call function with string name javascript
  18. call function with delay javascript
  19. call function with button javascript
  20. call function with pointer parameter c++