开发者

JavaScript passing a string into a method, which is an a attribute

开发者 https://www.devze.com 2023-02-08 02:48 出处:网络
I have the following code: myFunction(\'test\') I wish to set this on the onclick of an html element <a href=开发者_如何转开发\"#\" onclick=\"myFunction(\'test\')\" >Link</a>

I have the following code:

myFunction('test')

I wish to set this on the onclick of an html element

<a href=开发者_如何转开发"#" onclick="myFunction('test')" >Link</a>

I need to capture this as a string and add it via JavaScript as an inner element. How can I capture this?

"<a href="#" onclick="myFunction('test')" >Link</a>"
'<a href="#" onclick="myFunction('test')" >Link</a>'

Neither seem to work.


Do you mean you want to capture 'test' as a string, and pass it as the parameter? Try using backslahes before the single quotes surrounding test

<a href="#" onclick="myFunction(\'test\')" >Link</a>


Something like...

HTML:

<a href="#" onclick="myFunction('test')" >Link</a>

SCRIPT:

<script type="text/javascript">
function myFunction(userInput){ 
   document.getElementById('myNewHTMLElementID').innerHTML = userInput;
}
</script>


i think

myFunction('test')

should be

myFunction(test)

if you want to use test as variable in the function.

The function call itself looks OK.

0

精彩评论

暂无评论...
验证码 换一张
取 消