2

I want to select all the inputs with name attribute end with *Name.

I have 6 inputs type text with name :

  • deviceName
  • profileName
  • ssidName
  • captiveName
  • trafficName
  • sessionName

HTML

<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">

I'm trying to prevent any space enter on those 5 inputs

I've tried

$("input[name='*Name']").keyup(function() {
    this.value = this.value.replace(/\s/g, '');
});

My selection doesn't seem to take any effects.

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604

3 Answers3

2

I think you are using the correct selector but in the wrong place.

you need to use it like so $("input[name*='Name']")

See the link to jquery documentation. https://api.jquery.com/attribute-contains-selector/

tmkiernan
  • 375
  • 1
  • 16
2

Just a small error in your code with the attribute contains selector -

Change:

$("input[name='*Name']")

to

$("input[name*='Name']")

Check this JSBin for playing around.

Vandesh
  • 6,368
  • 1
  • 26
  • 38
1

To select element ending with attribute value 'Name' use $("input[name$='Name']") - https://api.jquery.com/attribute-ends-with-selector/.

$("input[name$='Name']").keyup(function() {
  this.value = this.value.replace(/\s/g, '');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">

Note - If you want to make attribute selector case-insensitive add i before closing square bracket i:e $("input[name$='name' i]").

console.log($("input[name$='name' i]"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">
random
  • 7,756
  • 3
  • 19
  • 25