start function when div appear [duplicate] - javascript

This question already has answers here:
How to add onload event to a div element
(26 answers)
Closed 8 years ago.
In my HTML file I have a div with id="print" and when this div appears on the screen I would like to start function print(). How can I do this?
<div id="print">
</div>
function check(){
//check appear div
}
function print(){
//print data
}

like #TheShinyTuxedo suggested this can be resolved by adding a script right after the div is rendered.
look like that:
<div id="print">
</div>
<script type="text/javascript">
print();
</script>

Related

How to apply javascripts to element wich has been overwrited by jquery .html() [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 5 years ago.
Here I have an example:
$('#key').on('click', function(){
$('.task').html("<button id='key'>Button</button>"+Date());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='task'>
<button id='key'>Button</button>
</div>
How I can apply javascript for overwriting element and get different time for each button press?
Clearly there is no need to ovveride button here. Do not ovveride button. Use a span for date.
$('#key').on('click', function(){
$('.datestr').html(Date());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='task'>
<button id='key'>Button</button>
<span class='datestr'></span>
</div>

javascript code is not executing if i open the link in new tab [duplicate]

This question already has an answer here:
.click() event when 'Open in new tab/window'
(1 answer)
Closed 5 years ago.
html code:
<div class="dummy">
test
</div>
javascript Code:
$('.dummy').on('click',function(e){
alert('good moring');
});
The above javascript code is not executing if i open the link in new tab,can someone help me out..
You have a a element inside your DIV. SO in order to have your alert just prevent the default action of anchor element using
e.preventDefault();
Read more on event.preventDefault()
$('.dummy').on('click', function(e) {
e.preventDefault();
alert('good moring');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dummy">
test
</div>

Replace div with new div using jQuery or javascript [duplicate]

This question already has answers here:
Replace Div with another Div
(3 answers)
Closed 6 years ago.
I need a simple solution for replacing a div with a new div, cross-browser compatible using javascript or JQuery. I'll add some code below. Div "myDiv-B" needs to be replaced by a new div:
<div id="myDiv-C">{% include 'snippets/contactpaneel.rain' %}</div>
Here are my divs
<div id="myDiv-A">
<div id="myDiv-B"></div>
</div>
You should use .replaceWith() method.
.replaceWith method replace each element in the set of matched
elements.
$('#myDiv-B').replaceWith('<div>Hello</div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv-A">
<div id="myDiv-B"></div>
</div>

How can I define something like OR in jQuery's selector? [duplicate]

This question already has answers here:
jQuery OR Selector?
(6 answers)
Closed 6 years ago.
Here is my current code:
$(".close_btn").click(function(e) {
$(".box1").fadeOut(100);
$(".box2").fadeOut(100);
});
As you see, currently, when you click on .close_btn, both .box1 and .box2 will be hidden. Now I want to use $(this) and .closest() to hide just one box (the parent of clicked .close_btn, there are two .close_btn). I mean I want something like this:
$(".close_btn").click(function(e) {
$(this).closest(".box1" OR ".box2").fadeOut(100);
});
Is doing that possible?
You can just use a comma to separate multiple selectors:
$(".close_btn").click(function(e) {
$(this).closest(".box2, .box1").fadeOut(100);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box1">
<button class="close_btn">Close</button>
</div>
<div class="box2">
<button class="close_btn">Close</button>
</div>

Can we add an href attribute on div tag? [duplicate]

This question already has answers here:
Can I call a code behind function from a div onclick event?
(3 answers)
Closed 6 years ago.
I have a div tag and it has a link tag inside it as shown in the code. The link works only when I click on it. But I want to code in such a way that even when I click on div tag, it should go to the link. Please help as I am a beginner in javascript.
<div id="blockbuilding">
<a id="#data.bbuildingid" href="#Url.Action("Overview","Overview", new{BuildingId=#data.buildingID})" class="buildingAnchor""> #data.buildingName </a>
</div>
try this
$(document).ready(function () {
$("#blockbuilding").click(
function () {
AlertSave();
}
);
});
function AlertSave() {
alert("Alert Message OnClick");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="blockbuilding">
<a id="#data.bbuildingid" href="#Url.Action("Overview","Overview", new{BuildingId=#data.buildingID})" class="buildingAnchor""> #data.buildingName </a>
</div>

Categories

Resources