2

I need to check if element (#idname) exists, then alert a message to user before closing current tab (or browser).

Here is my condition:

if (document.contains(document.getElementById("#idname"))) {
    window.onbeforeunload = function(evt) {
      // some code here
}

The above code works as well, but my problem is that element (#idname) isn't exist in the HTML in first and I will append it to my HTML after page loading. Now that condition doesn't work anymore. Is there any solution?

7
  • Define the handler after appending the element. Commented Jan 19, 2016 at 1:06
  • @Vohuman What do you mean "handler" exactly? Commented Jan 19, 2016 at 1:08
  • You should use getElementById("idname") instead of getElementById("#idname"), shouldn't you? The "#idname" is jQuery syntax. Commented Jan 19, 2016 at 1:10
  • you should run your js code after your element has inserted, how is it getting there, something is putting it it in,js maybe ? Commented Jan 19, 2016 at 1:11
  • Possible duplicate of Is there an "exists" function for jQuery? Commented Jan 19, 2016 at 1:13

2 Answers 2

2

You can simply check the existence of the element in your onbeforeunload handler:

window.onbeforeunload = function(evt) {
   if ( document.getElementById("idname") ) {
      // ...
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Can I ask another question? (it isn't related to the above question, it is a small question which doesn't has enough worth to I ask it on SO)
Thanks (my Iranian friend) ..! Well I'm trying to create a small markdown editor (something like SO's textarea), Here is a small demo of that. There is just one button (named Bold) which appends two stars ** around selected/highlighted text.
Now, what's the problem? That is: bad-handling-spaces-around-highlighted-text ..! To solve that problem, I have created a regex to handle spaces as well like this: $textarea.val( $textarea.val().replace(/\s*(\*+)\s*(.*?)\s*(\*+)\s*/g,' $1$2$3 ') );
The above regex should be paste on line 13 of that fiddle and it works as well. But it has a new problme, When I use that regex, the text-highlighting hides after clicking on Bold, Well do you know any workaround to I have both text-highlighting and using that regex?
The google CDN has been filtered in Iran and the demo isn't run properly for me. Well, let me answer your question tomorrow, It's 5 a.m. and I'm sleepy right now.
2

just check if you can select it:

$("#idname").length > 0

or

document.getElementById("idname") === null

You have to do the request inside your function! (See answer of Vohuman)

4 Comments

It's just an alternative of the OP's current code, it doesn't provide a solution.
Yes, @Vohuman is right, .length and === null exactly the same with .contains ..
you are right, your answer is what should solve the problem.
And please remove # in this line : document.getElementById("# ..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.