Get image clicked element with Jquery [duplicate] - javascript

This question already has answers here:
Get selected element's outer HTML
(30 answers)
Closed 9 years ago.
I want to be able to get ANY element's html code when I click on it.
So far I can only get text elements but I can't get image elements.
Here's what I have :
$(document).click(function(event) {
alert($(event.target).html());
});
this doesn't work with images tags like
<img src="" alt=""/>
but only with text ..
Can you please tell me how to proceed to get the images element ?
Thanks in advance.

I think you need to use outerHTML as fields like image/input(self closing) does not contain html
$(document).click(function(event) {
alert(event.target.outerHTML);
});

Related

how to run a function after html value change [duplicate]

This question already has answers here:
jQuery Event : Detect changes to the html/text of a div
(13 answers)
Closed 5 years ago.
I am doing a simple hover scrollbar project. I want to change the height of thumb when someone change the contents in html tag by prepend(),append(),html()..etc. how can I do this? I googled it and found answers like
$('.class').html('value').trigger(somefunction())
But it is not the answer. Because I am not the developer of other js codes. I want to do this with only my script file.
here is my project :-
https://github.com/rameshkithsiri/hoverscroll
Here is way of doing so , you can use DOMSubtreeModified events. But look out It before using it.
$("body").on('DOMSubtreeModified', ".myCLASS", function() {
alert($(this).html());
});
setTimeout(function(){ $(".myCLASS").html("my Test") }, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myCLASS"></div>

Javascript not working in div tag when div tag is add after run [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
when I click and blur div, an other div is create in last, but when I blur div what just is add, javascript not working, please tell me, why? and how fix.
Thanks for advance.
Sample is:
http://jsfiddle.net/a4QNB/420/
js:
var contents = $('.changeable1').html();
$('.changeable1').blur(function() {
$('#addItem').before('<div class=\"changeable1\" contenteditable=\"true\"> Click this div to edit it </div>');
});
The new div is getting dynamically added, so try to delegate the event.
var contents = $('.changeable1').html();
$('body').on('blur','.changeable1',function() {
$('#addItem').before('<div class="changeable1" contenteditable="true"> Click this div to add an other div </div>');
});
Note: You are using jquery 1.6. on is not available with this version
JSFIDDLE

Get div attribute value on page load [duplicate]

This question already has answers here:
How do I check if an element is hidden in jQuery?
(65 answers)
Closed 8 years ago.
I want to get value of div attribute. Like I want to know whether the div is "display:block;" or "display:none" at the time of page load. If "display:block;" then it will be "display:block;" after the page load again if not then "display:none". Can anyone help me with jquery method or code?
Do this:
<script>
$(document).ready(function(){
var x =$("#divId").css("display");
alert(x);
});
</script>

how to visit my site by .xyz.com/#divname, can any one help me? [duplicate]

This question already has answers here:
How to scroll an HTML page to a given anchor
(17 answers)
Closed 8 years ago.
I want browse directly to my site div by this way:
www.xyz.com/#divname
some sites I saw , I know there is a way to do it ,can anyone tell me the code
I have tried
window.location.href=#divname
but not scrolling.
Try
window.location.hash = '#divname';
Where #divname is the id of your div
Can you try this,
<body onload="window.location.hash = '#divname';">
That is called bookmark links, see here http://www.hyperlinkcode.com/bookmark.php.
To navigate to an anchor via javascript you can use this code:
window.location.hash = "#divname";
This topic is also a duplicate of the following:
How to scroll HTML page to given anchor using jQuery or Javascript?
Regards.
If you want to browse directly using a URL (as per your question), then there is absolutely no need for any javascript.
All you need to do is set an id for your div elements, that match the anchor tag you want to use. In the case of your example:
www.xyz.com/#divname
Then you would need an element like so:
<div id="divname">
</div>
Here is a working example

Element destination [duplicate]

This question already has answers here:
Retrieve the position (X,Y) of an HTML element
(30 answers)
Closed 9 years ago.
I created a html page and inserted a textbox. I would like to know a textbox destination in the page from javascript function? Example textbox's top is 100 px like that. Thank you.
Try this:
window.onload = function() {
console.log(document.getElementById("id1").offsetTop);
};
where id1 is the id of the textbox you need to triangulate.

Categories

Resources