1

The function used in below program.... Why we use single quote in function mopen('m1') instead of mopen(m1)

<ul id="sddm">
    <div id="m1" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">
        <a href="#">HTML DropDown</a>
        <a href="#">DHTML DropDown menu</a>
        <a href="#">JavaScript DropDown</a>
        <a href="#">DropDown Menu</a>
        <a href="#">CSS DropDown</a>
    </div>
</li>
<li>
    <a href="#" onmouseover="mopen('m1')" onmouseout="mclosetime()">Home</a>
Liath
  • 9,913
  • 9
  • 51
  • 81
Idris
  • 381
  • 2
  • 7
  • 16

4 Answers4

8
  • mopen(m1) gives the m1 declared viarable as an argument to the function mopen.

  • mopen('m1') gives the string literal 'm1' as an argument to the function mopen.

UPDATE

Updating accordingly to @Frogmouth's and @MazeHatter's comment :

In your case mopen(m1) will trigger a ReferenceError: m1 is not defined unless the variable m1 is defined somewhere as a global in your javascript code.

Community
  • 1
  • 1
Florian F.
  • 4,700
  • 26
  • 50
2

That would of course depend on what mopen() does. And strangely enough, I must've borrowed the same code as you for a project, mopen() looks like this:

function mopen(id) {

// cancel close timer
mcancelclosetime();

// close old layer
if(ddmenuitem != null) ddmenuitem.style.visibility = 'hidden';

// get new layer and show it
ddmenuitem = document.getElementById(id);
ddmenuitem.style.visibility = 'visible';

} 

You see here that the parameter you sent is id, which then gets sent to document.getElementById(id);

getElementById() expects a string, the id of the menu item in the HTML.

In this case its

<div id="m1"

So you pass in the string "m1".

Without the quotes m1 is a variable reference. Since you have no variable called m1, it would cause an error if you tried to read it.

MikeHelland
  • 1,151
  • 1
  • 7
  • 17
0
mopen('m1'); //you pass m1 as a string

mopen(m1); //mp1 an object or a variable
coolguy
  • 7,866
  • 9
  • 45
  • 71
0
 var m1="Shoaib"
  1. mopen(m1);
  2. mopen('m1');
  3. mopen();

    function mopen(methodOpen){
    
     alert(methodOpen+" is opened");
    }
    

First case output will be Shoaib is opened in alert box as m1 is consider as variable which you are passing to method.

Second case you are passing direct value as String 'm1' so output will be m1 is opened in the alert box.

Third case as you are not passing any variable so it will take as undefined So output will be undefined is opened

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70