0
 var $div = $('<div/>', {
         css: css,
         id: "testId" + product.id
 });

How can I access this id from this $div..

For now I hardcoded it when fetching from html, like this:

$('#testId_a2732d9e-db81-4c2f-85aa-563856e53ff4').css('background', '');

But I would like to access it directly in my javascript file from this $div

Thanks

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102

3 Answers3

1

Use the attr() method to get any attribute value.

$div.attr('id')
0

As long as you have access to this variable, then

$div.attr('id')
Thanh Trung
  • 3,566
  • 3
  • 31
  • 42
0

You can use .prop():

var product = { id: "abcd" };

var $div = $('<div/>', {
   id: "testId" + product.id
 });
 
 console.log($div.prop('id'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thanks for help, but now with this modifications I got this error ` $div.prop(...).css` is not a function – Roxy'Pro Nov 23 '19 at 14:32
  • 1
    @Roxy'Pro You said you wanted to get the ID. The ID is a string, so it doesn't have a `.css()` method. What are you trying to do? – JLRishe Nov 23 '19 at 14:56