0

I am trying to get an element by its value attribute.

Here is the code I'm using -

jQuery(document).ready(function () {
    jQuery('form input#attribute2').on('focus', function () {
        var $this = jQuery(this);
        var $valueofbox = $this.val();
        var $form = $this.closest('form');
        console.log($form);
        console.log($valueofbox);
        var $samebox = $form.find('input.rowvar2[value=$valueofthis]');
        console.log($samebox);
    });
});

I am trying to get the input field with the value of currently selected input box. This code works when I use -

$form.find('input.rowvar2[value=XXL]');  //notice that the quotes are not required here and it still gets the desired element

instead of

$form.find('input.rowvar2[value=$valueofthis]');

or

$form.find('input.rowvar2[value=$this.val()]');

What can be done here to solve this problem? Is there a better way to do this?

HTML code -

<form role="form">
    <div class="col-xs-2">

        <label for="attribute2">Size</label>

        <input type="text" class="form-control" id="attribute2" value="XXL">
        <br>

        <input type="text" class="form-control" id="attribute2" value="M">
        <br>

        <input type="text" class="form-control" id="attribute2" value="XXXL">
        <br>

<div class="row">
<!-- some HTML -->
    <div class="col-xs-3">
        <input type="text" class="form-control rowvar1 e27a8e79-ba6a-449f-a64b-e94baf1a098d" placeholder="Blue" disabled="">
    </div>

    <div class="col-xs-2">

        <input type="text" class="form-control rowvar2" value="M" disabled="">
        <br>

        <input type="text" class="form-control rowvar2" value="L" disabled="">
        <br>

        <input type="text" class="form-control rowvar2" value="XL" disabled="">
        <br>
    </div>
</div>
<!-- some html -->

<div class="row">
<!-- some HTML -->
    <div class="col-xs-3">
        <input type="text" class="form-control rowvar1 e27a8e79-ba6a-449f-a64b-e94baf1a098d" placeholder="Blue" disabled="">
    </div>
    <div class="col-xs-2">

        <input type="text" class="form-control rowvar2" value="XL" disabled="">
        <br>

        <input type="text" class="form-control rowvar2" value="XXL" disabled="">
        <br>

        <input type="text" class="form-control rowvar2" value="XXXL" disabled="">
        <br>

        <input type="text" class="form-control rowvar2" value="5XL" disabled="">
        <br>
    </div>
</div>


  <button id="submittopost" type="Submit" class="btn btn-success pull-right">Publish Product</button>
</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Gaurav Rajput
  • 15
  • 1
  • 5

2 Answers2

0

I dont think there are any fancy string replacements in JS? Just concatenate manually

$form.find('input.rowvar2[value=' + $valueofbox + ']');
Joseph Young
  • 2,758
  • 12
  • 23
0

Try using:

$form.find("input.rowvar2[value=${valueofbox}]");
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497