How do I turn a function variable into a div id name?

Christopher Reid

This is what I have that works:

function content() {
   var dd = document.getElementById("stuff1")
      dd.classList.toggle('disp');

}

<li onclick="content()"></li>

This is what I want to do:

function content(x) {
   var dd = document.getElementById("x")
      dd.classList.toggle('disp');

}


<li onclick="content(stuff1)"></li>

But that doesn't work. What is the proper way to accomplish this?

Brian North

Both other answers correctly state you need to remove the quotes from your getElementById call so that:

var dd = document.getElementById(x);

But you also need to add quotes to your content call in the onclick handler body:

<li onclick="content( 'stuff1' )"></li>

Otherwise the stuff1 is treated as a variable name and not a string.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I redirect the output of a system() function to a variable?

来自分类Dev

How do I send arduino a firmata command to turn on a pin

来自分类Dev

How do I turn logging off for Elasticsearch in Node.js?

来自分类Dev

How to call a script function with same div name in php?

来自分类Dev

puppet - How do I append to path variable?

来自分类Dev

How do I access a jQuery plugin variable from within method?

来自分类Dev

How do I use backend variable in ejs script section?

来自分类Dev

How do I find out the numeric ID of the current Postgres Transaction?

来自分类Dev

How do I get the matching id for every record?

来自分类Dev

How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

来自分类Dev

Retrieve value by variable name in erlang function

来自分类Dev

How to parse 'div' without name?

来自分类Dev

Swift: How do I return a value within an asynchronous urlsession function?

来自分类Dev

How do I use a Codeigniter method within my own function?

来自分类Dev

How do I use an extension function in another class? C#

来自分类Dev

How do I unit test a function that loops over objects?

来自分类Dev

How can I increment value of the variable when scroll reaches top of the div

来自分类Dev

编写div#id_name与#id_name的目的是什么?

来自分类Dev

Given the function `decode`, how can I do the inverse and write a function to `encode`?

来自分类常见问题

In jQuery, how do I get the value of a radio button when they all have the same name?

来自分类Dev

How do I run a global npm program when an ubuntu program has the same name?

来自分类Dev

带有id =“ {name)”的div可以使用吗?

来自分类Dev

从DIV JavaScript中的几个元素更改ID和NAME

来自分类Dev

How do I make this javascript function wait until I've scrolled x pixels down the page?

来自分类Dev

I have a few function overloads, each of which takes a different subclass; how do I check which function to use for a given object?

来自分类Dev

How do I get an ng-show to work inside an ng-repeat on a variable declared outside that scope?

来自分类Dev

How do I read a cell in a google spreadsheet in google scripts targeting the spreadsheet id

来自分类Dev

How to get the procedure or function name at runtime?

来自分类Dev

How do I patch or modify a list inside of a function when running a unit test?

Related 相关文章

  1. 1

    How do I redirect the output of a system() function to a variable?

  2. 2

    How do I send arduino a firmata command to turn on a pin

  3. 3

    How do I turn logging off for Elasticsearch in Node.js?

  4. 4

    How to call a script function with same div name in php?

  5. 5

    puppet - How do I append to path variable?

  6. 6

    How do I access a jQuery plugin variable from within method?

  7. 7

    How do I use backend variable in ejs script section?

  8. 8

    How do I find out the numeric ID of the current Postgres Transaction?

  9. 9

    How do I get the matching id for every record?

  10. 10

    How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

  11. 11

    Retrieve value by variable name in erlang function

  12. 12

    How to parse 'div' without name?

  13. 13

    Swift: How do I return a value within an asynchronous urlsession function?

  14. 14

    How do I use a Codeigniter method within my own function?

  15. 15

    How do I use an extension function in another class? C#

  16. 16

    How do I unit test a function that loops over objects?

  17. 17

    How can I increment value of the variable when scroll reaches top of the div

  18. 18

    编写div#id_name与#id_name的目的是什么?

  19. 19

    Given the function `decode`, how can I do the inverse and write a function to `encode`?

  20. 20

    In jQuery, how do I get the value of a radio button when they all have the same name?

  21. 21

    How do I run a global npm program when an ubuntu program has the same name?

  22. 22

    带有id =“ {name)”的div可以使用吗?

  23. 23

    从DIV JavaScript中的几个元素更改ID和NAME

  24. 24

    How do I make this javascript function wait until I've scrolled x pixels down the page?

  25. 25

    I have a few function overloads, each of which takes a different subclass; how do I check which function to use for a given object?

  26. 26

    How do I get an ng-show to work inside an ng-repeat on a variable declared outside that scope?

  27. 27

    How do I read a cell in a google spreadsheet in google scripts targeting the spreadsheet id

  28. 28

    How to get the procedure or function name at runtime?

  29. 29

    How do I patch or modify a list inside of a function when running a unit test?

热门标签

归档