191

How do I use JavaScript variables as a parameter in a jQuery selector?

<script type="text/javascript">
$(function(){    
  $("input").click(function(){
    var x = $(this).attr("name");

    $("input[id=x]").hide();    
  });    
});
</script>

<input type="text" id="bx"/><input type="button" name="bx"/>
<input type="text" id="by"/><input type="button" name="by"/>

Basically what I want to do is to be able to hide the element which has an id that is equal to the name of the element that is being clicked.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
user225269
  • 10,743
  • 69
  • 174
  • 251

6 Answers6

291
var name = this.name;
$("input[name=" + name + "]").hide();

OR you can do something like this.

var id = this.id;
$('#' + id).hide();

OR you can give some effect also.

$("#" + this.id).slideUp();

If you want to remove the entire element permanently form the page.

$("#" + this.id).remove();

You can also use it in this also.

$("#" + this.id).slideUp('slow', function (){
    $("#" + this.id).remove();
});
Vins
  • 8,849
  • 4
  • 33
  • 53
  • 3
    It's important to note that the variable used to concatenate must be non-numerical, so do toString() if id is a number. – isync Oct 12 '15 at 17:41
  • IE 11 doesn't like this, $('#' + id).hide();, it says it's undefined. – Welshboy May 24 '18 at 14:14
  • This works fantastic. For the record, you can also do ```var id = '#' + n + '"_appendedtext";```, then ```$(id).hide();``` It seems to happily accept that ```#``` within the variable. – Parapluie Apr 19 '22 at 20:36
58
$(`input[id="${this.name}"]`).hide();

As you're using an ID, this would perform better

$(`#${this.name}`).hide();

I highly recommend being more specific with your approach to hiding elements via button clicks. I would opt for using data-attributes instead. For example

<input id="bx" type="text">
<button type="button" data-target="#bx" data-method="hide">Hide some input</button>

Then, in your JavaScript

// using event delegation so no need to wrap it in .ready()
$(document).on('click', 'button[data-target]', function() {
    var $this = $(this),
        target = $($this.data('target')),
        method = $this.data('method') || 'hide';
    target[method]();
});

Now you can completely control which element you're targeting and what happens to it via the HTML. For example, you could use data-target=".some-class" and data-method="fadeOut" to fade-out a collection of elements.

Phil
  • 157,677
  • 23
  • 242
  • 245
15
$("input").click(function(){
        var name = $(this).attr("name");
        $('input[name="' + name + '"]').hide();    
    });   

Also works with ID:

var id = $(this).attr("id");
$('input[id="' + id + '"]').hide();

when, (sometimes)

$('input#' + id).hide();

does not work, as it should.

You can even do both:

$('input[name="' + name + '"][id="' + id + '"]').hide();
Alexx Roche
  • 3,151
  • 1
  • 33
  • 39
8
var x = $(this).attr("name");
$("#" + x).hide();
Alex R.
  • 4,664
  • 4
  • 30
  • 40
5

$("#" + $(this).attr("name")).hide();

morgar
  • 2,339
  • 17
  • 16
2
  1. ES6 String Template

    Here is a simple way if you don't need IE/EDGE support

    $(`input[id=${x}]`).hide();
    

    or

    $(`input[id=${$(this).attr("name")}]`).hide();
    

    This is a es6 feature called template string

        (function($) {
            $("input[type=button]").click(function() {
                var x = $(this).attr("name");
                $(`input[id=${x}]`).toggle(); //use hide instead of toggle
            });
        })(jQuery);
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <input type="text" id="bx" />
        <input type="button" name="bx" value="1" />
        <input type="text" id="by" />
        <input type="button" name="by" value="2" />
    
     

  1. String Concatenation

    If you need IE/EDGE support use

    $("#" + $(this).attr("name")).hide();
    

        (function($) {
            $("input[type=button]").click(function() {
                $("#" + $(this).attr("name")).toggle(); //use hide instead of toggle
            });
        })(jQuery);
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <input type="text" id="bx" />
        <input type="button" name="bx" value="1" />
        <input type="text" id="by" />
        <input type="button" name="by" value="2" />
    
     

  1. Selector in DOM as data attribute

    This is my preferred way as it makes you code really DRY

    // HTML
    <input type="text"   id="bx" />
    <input type="button" data-input-sel="#bx" value="1" class="js-hide-onclick"/>
    
    //JS
    $($(this).data("input-sel")).hide();
    

        (function($) {
            $(".js-hide-onclick").click(function() {
                $($(this).data("input-sel")).toggle(); //use hide instead of toggle
            });
        })(jQuery);
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <input type="text" id="bx" />
        <input type="button" data-input-sel="#bx" value="1" class="js-hide-onclick" />
        <input type="text" id="by" />
        <input type="button" data-input-sel="#by" value="2" class="js-hide-onclick" />
    
     
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242