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

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>

Related

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

JQuery click is not working [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
I have following simple html block:
<span class="change_ordering show-list" data-ordering-by="product_list" data-ordering-direction="desc">By list</span>
Then, I have following js-code:
$(document).ready(function() {
$('.show-list').click(function() {
console.log('qwe');
});
});
When I click on it - nothing happens. But when I paste this js-code to Google Chrome JS console, it works perfectly. JS works fine on my site, I can make other JQuery actions. I also tried to write $('.change_ordering.show-list') and $('span.change_ordering.show-list') and $('span.show-list'), but still no progress. What I'm missing?
EDIT: This element is not drowing by ajax. I don't understand why it's duplicate.
Please apply click using following way
$(document).on("click",".show-list",function(){})
It will work

Using innerHTML has no effect [duplicate]

This question already has answers here:
innerHTML not working javascript
(2 answers)
Closed 7 years ago.
I am trying to use javascript to dynamically change the header to my website and thus the header was not changing. My html, for simplicity, looks like
&ltdiv class="container"&gt
&lth1 id="name"&gtNic&lt/h1&g
&ltp id="email"&gt&lt/p&gt
&lthr&gt
&lt/div&gt
&ltscript src="js/something.js"&gt&lt/script&gt
My javascript lives in a file called something.js and looks like
$(document).ready(function() {
console.log('I got run');
$('#name').innerHTML = "YO";
$('#email').innerHTML = "why hello";
})
For some reason I see the log in the console but the html never changes. Why is the header not getting changed?
I did try looking at the stack overflow question
Javascript: Does not change the div innerHTML
and here Setting innerHTML: Why won't it update the DOM? and many others however none of them address my issue.
In jQuery you use .html("text") instead of .innerHTML
$(document).ready(function() {
console.log('I got run');
$('#name').html("YO");
$('#email').html("why hello");
})
Jquery doesn't have innerHTML property to set html instead it has method.
Use .text() if your content is only plain text
Like this
$('#name').text("YO");
If it has html content then use .html()
Like this
$('#name').html("<p>YO</p>");
DEMO

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>

Only one jQuery event runs per refresh [duplicate]

This question already has answers here:
Find currently visible div in jquery
(5 answers)
Closed 3 years ago.
Only one jQuery event runs per refresh of the page. What am I doing that's so wrong?
$("#contact_link_email").on("click",function(){
$("#contact-form").replaceWith('<h2>Email heading</h2>');
});
$("#contact_link_facebook").on("click",function(){
$("#contact-form").replaceWith('<h2>Facebook heading</h2>');
});
$("#contact_link_twitter").click(function(){
$("#contact-form").replaceWith('<h2>Twitter heading</h2>');
});
$("#contact_link_gplus").click(function(){
$("#contact-form").replaceWith('<h2>GPlus heading</h2>');
});
Once one of those events runs, $("#contact-form") doesn't reference anything. If you want to replace it with something, try making that something have the same id.

Categories

Resources