Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Selecting image

Tags:

jquery

primarily, i'm new for Jquery.

I have images like that . What i want is that when user click image,it makes image bordered. User can select a number of images. It all must be bordered when selected. After button is clicked, i will have images id.

  <tr><img id="i will put value for db processing"src="urlofimage"</tr> &nbsp;

enter image description here

How can i do this?

like image 378
Mert METİN Avatar asked Dec 06 '25 18:12

Mert METİN


2 Answers

Do you mean:


$.fn.hasBorder = function() {   
  if ((this.outerWidth() - this.innerWidth() > 0) ||  (this.outerHeight() - this.innerHeight() > 0)){
        return true;
    }
    else{
        return false;
    }
};
$(document).ready(function() {
  var selectedImgsArr = [];
   $("img").click(function() {

      if($(this).hasBorder()) {
          $(this).css("border", "");
          //you can remove the id from array if you need to
      }
      else {
         $(this).css("border", "1 px solid red");
         selectedImgsArr.push($(this).attr("id")); //something like this 
      }
   });
});
like image 175
Sudhir Bastakoti Avatar answered Dec 08 '25 07:12

Sudhir Bastakoti


a simple example:

live example in JsBin

the styling:

ul { list-style: none; }
ul li { display: inline; } 
ul li img { border: 2px solid white; cursor: pointer; }
ul li img:hover,
ul li img.hover { border: 2px solid black; }

the javascript:

$(function() {

  $("img").click(function() {      
    $(this).toggleClass("hover");      
  });

  $("#btn").click(function() {      
    var imgs = $("img.hover").length;    
    alert('there are ' + imgs + ' images selected');        
  });

});

the result:

enter image description here

like image 37
balexandre Avatar answered Dec 08 '25 08:12

balexandre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!