javascript - How to get div's ID with unique number in jquery?

474

I am using a jquery slider in my application.The input type hidden has got some image url paths. I want to get "

   img-paths- <?php echo $omsg_id; ?>

" $omsg_id value into var image_path_id = "#img-paths-"+$(this).data('id');

when i console printed in fire bug, the output was #img-paths-undefined

how to resolve it ?

  <div >
        <?php $imagePathArray = explode(',',$uploads); ?>
<input id="img-paths-<?php echo $omsg_id; ?>" type="hidden" value="<?php echo htmlentities(json_encode($imagePathArray)); ?>" />
    <div id="gallery7-<?php echo $omsg_id; ?>" ></div>
          <script>
   $(function(e) {

      var image_path_id = "#img-paths-"+$(this).data('id');
      var show_gallery_id = "#gallery7-"+$(this).data('id');
      console.log(show_gallery_id);
      console.log(image_path_id);

      $(show_gallery_id).imagesGrid({
        images:$.parseJSON($(image_path_id).val()),
        align: true,
        getViewAllText: function(imgsCount) { return 'View all' }
      });

    });

  </script>

         <!--<img src="<?php //echo $contentimage; ?>" alt="image post">-->
         <p><?php echo $message; ?></p>
       </div> 
9

Answer

Solution:

this refers towindow object, thus$(this).data('id') will not return the desired data.

You can directly use$omsg_id in script.

var image_path_id = "#img-paths-<?php echo $omsg_id; ?>";
var show_gallery_id = "#gallery7-<?php echo $omsg_id; ?>";

People are also looking for solutions to the problem: php - I want to overwrite cookie value according to the id

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.