-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,988
  • 2
  • 30
  • 47
vellai durai
  • 1,019
  • 3
  • 16
  • 38

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
  • 66,980
  • 10
  • 72
  • 94
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,774
  • 1
  • 20
  • 35
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,792
  • 1
  • 18
  • 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,987
  • 3
  • 32
  • 44
Ray
  • 280
  • 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,248
  • 2
  • 14
  • 26