JavaScript how to get the value when onclick and again on click value should be removed in jquery? value onclick javascript,value onclick,value onclick submit

I have a page where an anchor tag has id. When the anchor tag is clicked I am fetching the value to input type hidden and after again clicking on the same div it should be removed. The selection will be in a loop. Please check my code:

$(document).ready(function() {
    $(".get_category").on('click', function(){  
        var cat_id = $(this).attr('id');
        $("#categories"+cat_id).val(cat_id);
    })

});

https://jsfiddle.net/0zu17bn4/

Answer:1

you need to add these lines :

$(".get_category").on('click', function(){  
        var cat_id = $(this).attr('id');
        if ($("#categories"+cat_id).val() == '')
            $("#categories"+cat_id).val(cat_id);
        else
            $("#categories"+cat_id).val('');
})
Answer:2

Here is a fiddle:

on each click I check the value, if it is available I remove it if it's not there I set it.

Added: cat_id variable

https://jsfiddle.net/0zu17bn4/4/

$(".get_category").on('click', function(){  
        var cat_id = $(this).attr('id');
        $hiddenField =  $("#categories"+cat_id);
        if(!$hiddenField.val()){
            $hiddenField.val(cat_id);
        }
        else{
            $hiddenField.val("");
        }               
})
Answer:3

You have to make a condition on hidden input every time the anchor clicked to check if the value is empty or not, like following :

$(".get_category").on('click', function(){	
	
    var cat_id = $(this).attr('id');
    var input_hidden = $("#categories"+$(this).attr('id'));
    
    if(input_hidden.val()=="")
    	input_hidden.val(cat_id);
    else
        input_hidden.val("");
    
    alert("Hidden input value ="+input_hidden.val()); 
    
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2 col-sm-4 col-xs-6 home_s"> 
    <a class="get_category" id="36" target='_blank' href="javascript:void(0)">
        <img class="img-responsive img-center" src=""/>
        <span>test</span>
    </a>
</div>

<input type="hidden" name="categories[]" id="categories36" value=""/>
Answer:4

Using redux and react router, I would like to access a route parameter on a component other than the one specified by the route. I've got react-router, redux, and react-router-redux set up as ...

Using redux and react router, I would like to access a route parameter on a component other than the one specified by the route. I've got react-router, redux, and react-router-redux set up as ...

  1. react router route params
  2. react router route props
  3. react router route component
  4. react router route render
  5. react router route path
  6. react router route exact
  7. react router route config
  8. react router route key
  9. react router route change event
  10. react router route programmatically
  11. react router route matching
  12. react router route query params
  13. react router route vs link
  14. react router route on click
  15. react router route guard
  16. react router route not found
  17. react router route name
  18. react router route api
  19. react router route change
  20. react router route onenter

I am under the impression that ES6 classes are basically a syntactic sugar around the ES5 objects system. As I am trying to run React without a transpiler, I figured that I could use just use the old ...

I am under the impression that ES6 classes are basically a syntactic sugar around the ES5 objects system. As I am trying to run React without a transpiler, I figured that I could use just use the old ...

Is there a way to keep some sort of internal state while writing a curried function? For example, let's say I want to write a curried function that takes into account the number of times the function ...

Is there a way to keep some sort of internal state while writing a curried function? For example, let's say I want to write a curried function that takes into account the number of times the function ...

I am figuring out of i can make something like Google analytics script. They work with javascript to capture data and send it to an given URL. But i want it to do without ajax or REST API. What i ...

I am figuring out of i can make something like Google analytics script. They work with javascript to capture data and send it to an given URL. But i want it to do without ajax or REST API. What i ...