This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript - document.write error?
I have a piece of Javascript that I'm using that checks to see if a div is a certain style and then runs an if else statement:
window.onload = function () {
if (document.getElementById('slidingDiv').style.display == 'none') {
document.write('<img src="images/show-more-arrow.jpg" width="61" height="45">');
} else {
document.write('<img src="images/show-less-arrow.jpg" width="61" height="45">');
}
};
This works OK, but it only shows this HTML image on the screen. I need it to add the HTML to the existing code on the page.
Many thanks
Use appendChild or innerHTML
example:
document.getElementById("elemId").innerHTML = '<img src="images/show-less-arrow.jpg" width="61" height="45">'
var wrapper = document.createElement('div');
var image = document.createElement('img');
image.src='images/show-more-arrow.jpg';
wrapper.appendChild(image);
Something like this.
Related
This question already has an answer here:
Count Number of Clicks on a link (Without onclick) [closed]
(1 answer)
Closed 4 years ago.
i want to count clicks when this link is clicked. AND show "how many times(number) link clicked"in example.com
not to use mysql.and better php if not,then javascript is ok
example.com
For a pure JS implementation, you could use localStorage as follows:
<html>
<script>
function init() {
let count = localStorage.getItem('counter');
if(count === null){
count = 0;
localStorage.setItem('counter', count);
}
count = parseInt(count);
updateCount(count);
}
function incrementCounter() {
let count = parseInt(localStorage.getItem('counter'));
count = count + 1;
localStorage.setItem('counter', count);
updateCount(count);
return true;
}
function updateCount(count) {
document.getElementById("count").innerHTML = "Clicked "+count+" times!";
}
</script>
<body>
<p id="count">-</p>
<script type="text/javascript">
init();
</script>
Google
</body>
This question already has answers here:
GET URL parameter in PHP
(7 answers)
Closed 6 years ago.
I'm trying to get page number from web address like beds.html?page=3
but, if I use php get method, then it's not returning anything.
I hope to find any easy solution.
cheers
If You Want that parameter on html page then you have to use javascript for it
<script>
var param1var = getQueryVariable("page");
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>
if you are on php page simply use
<?php
echo $_GET['page'];
?>
you can get page like this:
<?php
$_REQUEST['page'];
?>
and also you can type echo before $|_REQUEST to check you are getting proper page number or not
get is the right method to use.
<?php
$_GET['page'];
?>
This question already has answers here:
how to convert javascript variable to PHP variable? [closed]
(2 answers)
How to get JavaScript function data into a PHP variable
(5 answers)
Closed 9 years ago.
i am trying to get the javascript variable value to php.
here is my javascript code:
function getResults()
{
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++)
{
if (radios[i].checked) {
var a = radios[i].value
alert(a);
break;
}
}
}
from this javascript i want to get variable a's value inside php code onclick on submit button.
how can i do this?
i tried this
$var1 = $_GET["a"];
Do this..
$var1 = <?=$_GET["a"]?>;
to communicate a value from JavaScript to PHP do following
$var1 = <?=$_POST["a"]?>;
What you tried is almost correct:
var a = <?=$_GET["a"]?>;
Since javascript code runs on client-side, but PHP is server-side code, you need to send your JS variables to the server. You can do this using AJAX.
Hint: AJAX calls usually use POST instead of GET.
Replace:
$var1 = $_GET["a"];
on:
$var1 = <?=$_POST["a"]?>;
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
random images display
Have anyone see an image or other content that changing randomly when we reloading the page?
What is that? Can you tell me how to do that?
Guess, something like this would do the trick.
/* div for your future image */
<div id="img"></div>
<script>
window.onload = function(){
/* array of urls to your images and random integer number between 0 and array's length-1 */
var imgurls = [url1,url2,url3,...,urlN],
randn = Math.floor(Math.random()*imgurls.length);
/* create an HTML tag <img src="urlN"> in your div */
document.getElementById('img').innerHTML = '<img src=' + imgurls[randn] + '>';
}
</script>
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Update a Select Box on Parent Page from FancyBox2
I'm trying to update an element on my site from a fancybox. In the fancybox I do a $.post() and get back html data that aI want to populate in a div on my page. I'm opening the fancybox in ajax. I can see in firebug that my script is returning the html but the select box on the parent page is not updating. Can anyone help me with the proper way to do that? I'm currently trying from the fancybox:
$("#send-message").click(function () {
$(this).closest('form').submit(function () {
return false;
});
var frm = $(this).closest('form');
if ($(frm).valid()) {
$("#ajax-loading").show();
var data = $(frm).serialize();
$(frm).find('textarea,select,input').attr('disabled', 'disabled');
$.post("../forms/company_add.php",
data,
function (data) {
if (data.success) {
$('#companyselect', $(parent.document)).html(data.success);
parent.$.fancybox.close();
} else {
$("#ajax-loading").hide();
$(frm).find('textarea,select,input').removeAttr('disabled');
$("#send_message_frm").append(data.error);
}
}, "json"
);
}
});
I'm using fancybox-2 and php. Thx!
I think you just need to use the id of the element
$('#companyselect').html(data.success);
$.fancybox.close();
Write a javascript function on your parent like
function UpdateElementOfParent(someValue)
{
// your code
}
And from your fancybox, you can the object of parent and call its function like that..
this.parent.UpdateElementOfParent(someValue);