3

It is possible to get boolean from this?

$(".svgobjects").on("mousedown", function(){
        console.log("clicked");
});

I want to check if user mousedown on one of divs with ".svgobjects" class.

EDIT:

why this code returns always "false"?

var ifclicked=false;
    $(".svgobjects").on("mousedown", function(){
        console.log("clicked");
        ifclicked = true;
    });
    console.log("ifclicked "+ifclicked);

This is screen from google chrome console log

Piotrek
  • 10,919
  • 18
  • 73
  • 136
  • I don't think it is possible like that.... one possible solution is to set a flag on mouse down and set it to false on mouse up – Arun P Johny Aug 28 '13 at 08:07
  • Duplicate of http://stackoverflow.com/questions/7700066/check-if-mousedown-with-an-if-statement – Manu M Aug 28 '13 at 08:08

6 Answers6

4

Store the boolean value to some var:

var clicked = false;
$(".svgobjects").on("mousedown", function(){
  clicked = true;
});
m1k1o
  • 2,344
  • 16
  • 27
4

You simply use a variable

var IsSvgobjectsMousedown = false;
$(".svgobjects").on("mousedown", function(){
        IsSvgobjectsMousedown = true;
        console.log("clicked");
});
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

Another possibility is Using data()

Store arbitrary data associated with the specified element. Returns the value that was set.

$(".svgobjects").on("mousedown", function(){
      $(".svgobjects").data("clicked",  "yes");
});

alert($(".example").data("clicked")); 

For specific object

 $(".svgobjects").on("mousedown", function(){
          $(this).data("clicked",  "yes");
    });

Live demo

with the data() you can store some data to the element when it being clicked.And retrieve that information ,When you need it :)

Edit after update the Question:

That means you are logging the variable ,before the callback,Which is updating the variable after the logging done.

$(".example").append("<input  class='svgobjects'</input>")
var ifclicked=false;
    $(".svgobjects").on("mousedown", function(){
        console.log("clicked");
        ifclicked = true;
        alert(ifclicked);
    });
    console.log("ifclicked "+ifclicked);

Demo for the Edit

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • but what if user end "mousedown" on my object? All of previously clicked objects will keep "clicked" "yes". – Piotrek Aug 28 '13 at 08:22
  • In such cases use `this` if you want to set to specific element.And regarding to your update in the question ,Is that element you are doing `mouse down` is dynamically added ??? If no use normal mousedown other wise see my demo. – Suresh Atta Aug 28 '13 at 08:24
1

The reason this is not working as you expect is because the code that you have written does not execute in a linear fashion. The '$(".svgobjects").on("mousedown",...' block is an event listener with a callback that will only fire once that event occurs.

If you are looking at performing a task once something is clicked, perhaps wrap it in a function and call that function within the mousedown callback.

J4CQ
  • 36
  • 2
0

you will always receive false that ways, you code order is not right

var ifclicked=false;
    $(".svgobjects").on("mousedown", function(){
        console.log("clicked");
        ifclicked = true;
        console.log("ifclicked "+ifclicked);
    });

Now it will return true, you earlier sequence wasn't correct

Maverick
  • 478
  • 1
  • 3
  • 13
0

You should move the last code into the callback like below,

var ifclicked=false;
$(".svgobjects").on("mousedown", function(){
    console.log("clicked");
    ifclicked = true;
    console.log("ifclicked: "+ifclicked);
});
JerryChang
  • 16
  • 2