-3

I am using

 <div id="123456ABC" class="classname" onclick="javascript:AddValue(aa.value,'33',bb.value,'1000')"></div>
<div id="78904 bbc" class="classname1" onclick="javascript:AddValue(aa.value,'55',bb.value,'2000')"></div>

I need to remove the spaces and text after space in the id if space exist in the id

in the above case i need the id as id=123456ABC and id=78904

Steve
  • 2,968
  • 2
  • 31
  • 47
vellai durai
  • 959
  • 3
  • 13
  • 29

5 Answers5

2

try this

$( "div" ).each( function(){
   var id = $(this).attr("id");
   var spaceIndex = id.indexOf(" ");
   id = spaceIndex == -1 ? id : id.substring(0, spaceIndex);
  $(this).attr("id", id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="123456ABC" class="classname" onclick="javascript:AddValue(aa.value,'33',bb.value,'1000')"></div>
<div id="78904 bbc" class="classname1" onclick="javascript:AddValue(aa.value,'55',bb.value,'2000')"></div>
gurvinder372
  • 64,240
  • 8
  • 67
  • 88
0

You can use below script to remove inner space.

str = str.replace(/\s/g, '');

more information provided here:-

How to remove spaces from a string using JavaScript?

Community
  • 1
  • 1
Deepak Dholiyan
  • 1,559
  • 1
  • 15
  • 34
0

If you want to remove spaces from all div then you can try code below:

$.each($(div), function(k, v) {
     var id = $(this).attr('id');
     var temp = id.split(" ");
     $(this).attr('id', temp[0]);
});

Hope, it will help you.

Lovepreet Singh
  • 4,662
  • 1
  • 15
  • 36
0

You can use the following code to strip out the spaces (assuming you have the ID in a variable called originalId):

var noSpaceId = originalId.replace(/ +/g, "");
Evan Porter
  • 2,969
  • 3
  • 33
  • 43
Ray
  • 272
  • 3
  • 4
0

Please check below code to remove string after space for all the divs.

$(document).ready(function(){
  $('div').each(function(){
    $(this).attr('id',(this.id).split(' ')[0]);
  });
})

Please look in to https://jsfiddle.net/2kaak2ur/

Rahul Patel
  • 5,178
  • 2
  • 12
  • 25