Passing parameters in java script function ?

user3475960

I m new in javascript developpement i m trying to make e function which hide or show some text in a span after a specific mouse event and i want to pass the id of this span in my function this is my script which work with only a specific id which is "sp1" in this case but i want to pass the id in my function parametres

<script>function myOverFunction() {
document.getElementById("sp1").style.visibility = 'visible';}
function myOutFunction() {
document.getElementById("sp1").style.visibility = 'hidden';}

and this a part of my html code

<div id="lg1"onmouseover="myOverFunction()" onmouseout="myOutFunction()"></div>
<center>
<div>
<span id="sp1" style="visibility: hidden;" >Current Value :70 <br>Target :80 <br>
<div class="label label-blue">In progress 90%</div>
</span>
</div>
</center>
NickSlash

Passing a parameter to a function like that can be achieved by using either single or double quotes depending on what you've used to enclose the code in your html.

<div id="mydiv" onclick="myFunction('mydiv');"></div>

Doing this is not really the best way to do it though.

When "myFunction" is fired by the onclick event, by default an e (event) parameter is passed. This contains lots of information about the event that happened, including a reference to the object that fired it. So you can find the id of your element using it.

function myFunction(e) {
    e = e || window.event;
    alert(e.target.getAttribute("id"));
}

And your HTML would be...

<div id="myDiv" onclick="myFunction()"></div>

Edit:

Looking at your question a little further, you can combine this, and your two functions into one toggling function

function toggleVisibility(e) {
    e = e || window.event;
    if (e.target.style.visibility == "visible") {
        e.target.style.visibility = "hidden";
    } else {
        e.target.style.visibility = "visible";
    }
}

You can call the same function for onmouseover and onmouseout on any element providing the visibility property is not null (i think it defaults to visible? bit of a guess thouugh)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Passing arguments / parameters from a Google sheets custom function to the script function

分類Dev

Passing options/args/parameters with spaces from the script to a function within

分類Dev

Passing parameters to jest function

分類Dev

Golang: Passing structs as parameters of a function

分類Dev

JavaScript function not passing my parameters to other function

分類Dev

Passing an argument to a function in a different script?

分類Dev

Passing parameters through Ajax success function

分類Dev

Unpacking a dictionary and passing to a function as keyword parameters

分類Dev

Is there a way to call function without passing its parameters?

分類Dev

Passing a function that requires parameters as a callback in Kotlin

分類Dev

Snakemake - passing additional parameters to an input function

分類Dev

Passing windows slash (/) based parameters to a program from bash script

分類Dev

Passing data from Java process to a python script

分類Dev

Variadic Function in Java Script

分類Dev

How to pass parameters to function in a bash script?

分類Dev

Passing a function with parameters as columns within double loop in R

分類Dev

passing complex java class to pl/sql function

分類Dev

passing parameter from java script on input html form

分類Dev

Passing parameters to middleware in Laravel

分類Dev

Passing parameters to a custom URI

分類Dev

Passing parameters to AngularJS $timeout

分類Dev

Passing parameters to a custom URI

分類Dev

Passing LINQ expressions as parameters

分類Dev

Passing parameters to a OPENQUERY

分類Dev

passing BlocProvider with class parameters

分類Dev

Passing parameters to a react component

分類Dev

Error with passing parameters

分類Dev

Maple passing parameters by reference

分類Dev

Iframe not passing url parameters

Related 関連記事

  1. 1

    Passing arguments / parameters from a Google sheets custom function to the script function

  2. 2

    Passing options/args/parameters with spaces from the script to a function within

  3. 3

    Passing parameters to jest function

  4. 4

    Golang: Passing structs as parameters of a function

  5. 5

    JavaScript function not passing my parameters to other function

  6. 6

    Passing an argument to a function in a different script?

  7. 7

    Passing parameters through Ajax success function

  8. 8

    Unpacking a dictionary and passing to a function as keyword parameters

  9. 9

    Is there a way to call function without passing its parameters?

  10. 10

    Passing a function that requires parameters as a callback in Kotlin

  11. 11

    Snakemake - passing additional parameters to an input function

  12. 12

    Passing windows slash (/) based parameters to a program from bash script

  13. 13

    Passing data from Java process to a python script

  14. 14

    Variadic Function in Java Script

  15. 15

    How to pass parameters to function in a bash script?

  16. 16

    Passing a function with parameters as columns within double loop in R

  17. 17

    passing complex java class to pl/sql function

  18. 18

    passing parameter from java script on input html form

  19. 19

    Passing parameters to middleware in Laravel

  20. 20

    Passing parameters to a custom URI

  21. 21

    Passing parameters to AngularJS $timeout

  22. 22

    Passing parameters to a custom URI

  23. 23

    Passing LINQ expressions as parameters

  24. 24

    Passing parameters to a OPENQUERY

  25. 25

    passing BlocProvider with class parameters

  26. 26

    Passing parameters to a react component

  27. 27

    Error with passing parameters

  28. 28

    Maple passing parameters by reference

  29. 29

    Iframe not passing url parameters

ホットタグ

アーカイブ