0

I am getting id of div from external source and in that spaces also coming in id , how to get the value of id. Here is my div example:

<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 get the class name from the id. Here is what I am doing:

function AddValue(aa, bb) {
    var classOfDiv = $('#123456ABC').attr('class');
    var classOfDivs = $('#8904 bbc').attr('class');
    alert(classOfDiv);
    alert(classOfDivs);
}

The first alert is working fine but second is not fetching a value. How can I handle this? All the values are dynamic.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
vellai durai
  • 1,019
  • 3
  • 16
  • 38

1 Answers1

1

Use $("div[id='78904 bbc']") to access element which has spaces in id, Try:

var classOfDiv = $("div[id='123456ABC']").attr('class');        
var classOfDivs = $("div[id='78904 bbc']").attr('class');
alert(classOfDiv);
alert(classOfDivs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/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>
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27