Hello all. I am trying to get a function call from an onclick used in a html form
it looks like this.

function work(strVal) {

	alert(strVal);

	return 1;
}

and this

<a href='#'  onclick='work('word')'><img src='$RestaurantName' style='border-style: none' height='300' width='300' onMouseOver='mouseOver1()' onMouseOut='mouseOut1()'></a>

I don't know javascript very well but i do know java, and this should work with a string. When i use the work() function in a onclick it only works with an integer. If i use it anywhere else i can use and integer or string.


so if i do this it will cause a pop up alert that shows the integer correctly

<a href='#'  onclick='work(0)'><img src='$RestaurantName' style='border-style: none' height='300' width='300' onMouseOver='mouseOver1()' onMouseOut='mouseOut1()'></a>

If i do this it will not pop up 'word'

<a href='#'  onclick='work('word')'><img src='$RestaurantName' style='border-style: none' height='300' width='300' onMouseOver='mouseOver1()' onMouseOut='mouseOut1()'></a>

Is there something about an onclick that limits the use of a string in the functions arguement. this function works with a string if i call it from somewhere else, it just doesnt work in onclick . I have not clue thanks alot people.

Recommended Answers

All 5 Replies

helleo try this but here I use a button ratherthan image :
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("word!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

I tried it with code you put,it works successfuly:

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("word!");
}
</script>
</head>
<body>

<a href='#' onclick="show_alert()" ><img src='$RestaurantName' style='border-style: none' height='300' width='300' onMouseOver='mouseOver1()' onMouseOut='mouseOut1()'></a>
</body>
</html>

yes thank you the what about like this.

<html>
<head>
<script type="text/javascript">
function show_alert(name)
{
alert(name);
}
</script>
</head>
<body>

<a href='#' onclick="show_alert('name')" ><img src='$RestaurantName' style='border-style: none' height='300' width='300' onMouseOver='mouseOver1()' onMouseOut='mouseOut1()'></a>
</body>
</html>

that funcion i wrote will work normally although not i a onclick


thats where the problem is. I need to put a string in the functions method. It will only accept an integer. thanks

Jonnyboy12,

In your original post, the problem is with onclick='work('word')' which will throw javascript error due to single-quotes inside single-quotes.

Try onclick="work('word')" .

You may like to consider onclick="work('word'); return false;" , which will completely suppress the <a> tag's natural href action. This is useful even with href="#".

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.