Stuck with onclick and document.GetElementById - javascript

I just began JavaScript and I've been stuck for a few hours now with an onclick which is not working, or maybe it's the document.getElementById. What I want to do is hide the div when I click on it.
If anyone can explain me what I'm doing wrong I would be grateful!
function closing() {
var closecook = document.getElementById("cookies");
closecook.style.display = "none";
}
#cookies{
display: block;
}
<div id="cookies" onclick="closing()">
Our website is using cookies, click here to close this message.
</div>
Here's my relevant HTML markup:
<body>
<div id="cookies" onclick="closing()">
Our website is using cookies, click here to close this message.
</div>
</body>
<script type="text/javascript" src="functions.js"></script>
Thanks.

Place your "script" block at the end of your body, not outside of it!
alter you the function "closing" to something like this:
function closing() {
var closecook = document.getElementById("cookies");
console.log(closecook);
closecook.style.display = "none";
}
In your browser: open the console by hitting "F12" or right-click anywhere in your browser and select "inspect element" then choose "console".
Now execute your function by clicking on the div.
If you see a "null" in the console: the problem comes from "document.getElementById("");".
If you do not see anything pop up in the console, your javascript file is not loaded properly. Make sure there is no typo in the file name! (Linux is case-sensitiv!).

Related

Clicking AJAX links? Watir

SOLVED:
I figured out my problem with this little piece of code:
b.link(id: 'm_wm_w5_m_lv_ctrl1_m_lnkWatch').fire_event :click
QUESTION:
I am having trouble clicking AJAX links with Watir.
Here is what I want to click.
https://i.imgur.com/Md4Rha3.png
This is what HTML looks like
<a id="m_wm_w5_m_lv_ctrl1_m_lnkWatch" href="javascript:__doPostBack('m_wm$w5$m_lv$ctrl1$m_lnkWatch','')"
style="white-space:nowrap; font-size:11px;">New Listing (160)</a></td>
So, in Watir, I used the following to click to think
b.link(id: 'm_wm_w5_m_lv_ctrl1_m_lnkWatch').click
But when I do that, I get this CSS pop up:
https://i.imgur.com/2DKWAhF.png
The HTML for close button on the pop up is:
<div id="NewsDetailDismissNew" class="btn mtx-btn-confirm enabled" title="Close">Close</div>
But when I use this in Watir
b.div(id: 'NewsDetailDismissNew').click
Nothing happens.
I did find some javascript that seems to correspond with the buttons
<!--<div class="container" >-->
<script>
var cvMarkAsRead=function( ){ return "Mark as Read"; };
</script>
<script>
var cvClose=function( ){ return "Close"; }; //This is closes the CSS pop up as far as I can tell
</script>
<script>
var cvOK=function( ){ return "OK"; };
</script>
<script>
var cvDismiss=function( ){ return "I've Read This"; };
</script>
<script>
var cvPreview=function( ){ return "Print Preview"; };
</script>
<!-- End NewsDetail modal -->
My question to you guys is
How can I click "New Listings" in Watir?
How can I click the "Close" button on the pop up?
I see that the link has some javascript, but I am not familiar enough with Watir to use b.execute_script successfully. I read the documentation. I am not understanding it and the examples provided are not similar enough to my problem for me to learn by practicing or copying.
Thank you in advance.
The piece of code below solves my problem. It clinks on New Listing and successfully loads all the new listings as well as avoids triggering that erroneous CSS pop up.
I think what this code does is activate the javascript by clicking the link.
I successfully clicked this link
<a id="m_wm_w5_m_lv_ctrl1_m_lnkWatch" href="javascript:__doPostBack('m_wm$w5$m_lv$ctrl1$m_lnkWatch','')"
style="white-space:nowrap; font-size:11px;">New Listing (160)</a>
By using this code in Watir:
b.link(id: 'm_wm_w5_m_lv_ctrl1_m_lnkWatch').fire_event :click

File load popup to appear with function, input is hidden

I want to file load popup to appear after I use function loadFile(), because myInput is hidden.
//HTML
<div id ="hideWhiteSpace" style="display:none">
<input id="myInput" type="file"/>
</div>
Here is my javascript file example, onchange won't work cause i cant press the button.
//JS
function loadFile(){
document.getElementById('myInput').onchange = function (e){
//run code to load file into web.
}
}
Okay, I got it, just put this before onchange line:
document.getElementById('myInput').click();
I don't know if I should answer my own questions or delete the post if I got the answer.
Comment please.

Make a Popup/Alert Window in a html with a "dont show again" option

I am trying to make a popup / alert window so that when the page is being loaded, the popup will open. I searched around and found something I like, but I don't know how to get this option working with the ability to not show the popup to the user more than once (with a "Don't show this again" option).
I added this to my header in the script part:
$(document).ready(function(){ alert('hi')});
I know that I need the jQuery script for this, so I added
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
to my HTML page. This is working fine, but I don't know how I could modify my alert in a way for making a checkbox or a button with "Don't show this again".
I also found a solution where the alert was an external popup HTML page, but I want it inside my HTML page. Is there a way to solve my problem over that, or is the way over the alarm better?
Unfortunately, you can't do this through a typical JavaScript alert box. You'll need to build you own modal popup to simulate an alert box. jQuery's plugin jQuery UI has a really nice built-in function for this, and I'll use this in my example.
To give the user the option of not showing a window again, you need to make use of localStorage. You would need to create a condition that checks for whether a localStorage item is set. If it is not, display the modal, if it is, hide the modal:
if (!localStorage.hideAlert) {
$(function() {
$("#dialog").dialog();
});
}
else {
$("#dialog").css("display", "none");
}
In the modal itself, you would have a 'No' button that adds the relevant value to localStorage:
<div id="dialog" title="Show Again?">
<p>Would you like to show this dialog again?</p>
<button name="yes" class="yes">Yes</button>
<button name="no" class="no">No</button>
</div>
$(".yes").on("click", function() {
$("#dialog").dialog("close");
});
$(".no").on("click", function() {
localStorage.setItem('hideAlert', true);
$("#dialog").dialog("close");
});
I've created a working example showcasing this here.
This way, all of your code can reside within a single file, though remember that you'll still need to include the external jQuery UI JavaScript, and optional CSS:
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
Hope this helps! :)
In the example below, every popup window has a "Don't Show This Again" button.
Main document:
Code:
<HTML>
<Head>
<Script Language=JavaScript>
var expDate = new Date();
expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year
function setCookie(isName,isValue,dExpires){
document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}
function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}
function initPopups(){
if (!getCookie('pop1'))
{popWin1 = window.open("1/pop1.html","","width=200,height=150,top=50,left=400")}
if (!getCookie('pop2'))
{popWin2 = window.open("1/pop2.html","","width=200,height=150,top=50,left=180")}
}
window.onload=initPopups;
</Script>
</Head>
<Body>
</Body>
The popup files are in a folder named 1
pop1.html:
Code:
<HTML>
<Body>
<input type=button value="Don't show again" onclick="opener.setCookie('pop1',0,opener.expDate);self.close()">
</Body>
</HTML>
pop2.html:
Code:
<HTML>
<Body>
<input type=button value="Don't show again" onclick="opener.setCookie('pop2',0,opener.expDate);self.close()">
</Body>
</HTML>

Asp.net:show loading image before page loads for the first time

i am trying to display a loading image when the page is loading.Here is my code.
<body onunload="doHourglass();" onbeforeunload="doHourglass();" onload="setTimeout('show()',2000);">
<div id="divWait" style="display:none" >
<h1>wait...</h1>
</div>
<div id="main" style="display:none">
<asp:Button ID="btnHidden" runat="server" OnClick="code behind handler"
<div/>
<script>
function doHourglass()
{
console.log("inside dohour glass");
var divwait=document.getElementById('divWait');
var divmainpage=document.getElementById('main');
divmainpage.style.zIndex="0";
divwait.style.zIndex="1";
divwait.style.display='inline';
divmainpage.style.display='none';
setTimeout("show()",2000);
}
function show()
{
console.log("inside show");
var divwait=document.getElementById('divWait');
var divmainpage=document.getElementById('main');
divmainpage.style.zIndex="1";
divwait.style.zIndex="0";
divwait.style.display='none';
divmainpage.style.display='inline';
}
</script>
</body>
Problem is, it is not working when the page is loading for the first time.when i checked using console.log() i see for the first time control is not going inside doHourGlass() function.but for button click event it is working perfectly.
Things i tried:checked this link on SO, but i am not putting static html.I am trying to get the id of an html element and then working on that element so i can not use that solution.
Edit:one way i found is to keep divwait visible and hide main division then show main div when the page loads.but this way i can only display static html.i want to be able to call a function when page is loading for first time.
what is happening?is there a way to achieve it?please guide me.

Javascript form.submit() works only when debugging

I have a weird problem when I try to submit a form. My page contains a html table and one column has a link that deletes the line from the DB calling a js function.
The html/php line is the following one:
<input type="hidden" name="deleteid" value=0>
print ", delete";
The javascript function is the following one:
function delete_line(team_id) {
if (confirm('<?php echo $txt_confirm_del_team;?>')){
document.formName.elements.deleteid.value = team_id;
document.formName.submit();
}
}
If I put a breakpoint on the line "document.formName.submit();", the called page can read the parameter "deletedid". If not, the parameters are not passed to the page called by the submit.
Btw, I have another issue if I use getElementById() to retrieve the element or the form in the javascript function: it says the getelementbyid is null (and it's not because I can retreive it using the syntax document.formName.elements.deleteid)
thanks
There may be some other event handlers there, you can open the page in Chrome and right click the anchor, then choose "inspect element" and then check out the event handlers in the "event listeners" tab.
As far as the code you posted; there is nothing wrong with it. The following works just fine for me:
<html>
<head>
<title>test</title>
</head>
<body>
<a onclick="javascript:delete_line(22);">delete</a>
<script>
function delete_line(nr){
console.log(nr);
}
</script>
</body>
</html>

Categories