4

I've a CSS id which shows a small red ball and a JAVASCRIPT function. I want to pass the CSS id as a parameter in the JavaScript function. I've seen a lot of tutorials but can't figure it out. Here are the codes:

CSS code

#projectile{
    position: absolute;
    background-color: #ff0000;
    left:176px;
    top: 486px;
    width:10px;
    height:10px;
    border-radius: 8px;
    z-index: 9;
}

JAVASCRIPT code

function gofish(projectile_id){
var projectile = getElementbyId(projectile_id);
start_x = projectile.offset().left;
start_y = projectile.offset().top;

function init()
{ setTimeout("gofish('projectile')", 500); }
5
  • Do you have a getElementbyId function, or did you actually mean document.getElementbyId Commented Mar 29, 2014 at 6:50
  • And it looks like you're using jQuery from the offset() function ? Commented Mar 29, 2014 at 6:50
  • Open up the browser console, you'll find it useful to viewing error messages. Commented Mar 29, 2014 at 6:52
  • @adeneo, I tried with both getElementbyId and document.getElementbyId but nothing worked. And yes I'm using jquery offset function. Commented Mar 29, 2014 at 6:55
  • @user2611329 - I thought so, and I've posted an answer below Commented Mar 29, 2014 at 6:59

3 Answers 3

2

Lets assume you're using jQuery and that you want a jQuery object to use the offset() method as no such method exists for plain DOM nodes

function gofish(projectile_id){
    var projectile = $('#' + projectile_id);
    var start_x = projectile.offset().left;
    var start_y = projectile.offset().top;
}

function init() { 
    setTimeout(function() {
        gofish('projectile');
    }, 500); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

this worked. Just it should be projectile = $('#' + projectile_id); instead of var projectile = $('#' + projectile_id);. Tnx, it helped a lot! :)
That would make the variable global, and you should generally avoid that, but if it works it works. Happy to help!
2

You have a few mistakes in your JavaScript.

  • getElementById should have a capital 'b', and should be called against document.
  • I think the offset properties you are look for are element.offsetLeft and element.offsetTop.
  • You can define an anonymous function directly on setTimeout to call init(), exactly as adeneo has suggested.
  • You will still need to call init() somewhere in order for it to run.

Here is updated JavaScript code:

function gofish(projectile_id) {
    var projectile = document.getElementById(projectile_id);
    start_x = projectile.offsetLeft;
    start_y = projectile.offsetTop;
}

function init() {
    setTimeout(function () {
        gofish('projectile');
    }, 500);
}

init();

And here is the code in action, so far: http://jsfiddle.net/JuvDX/

Comments

0

This may help you little..

    <!DOCTYPE html>
<html>
<head>

<script>
function myFunction(element)
{
alert(document.getElementById(element.id).value);
}
</script>
<style>
#para1
{
text-align:center;
color:red;
} 
</style>
</head>

<body>
<p id="para1" onclick="myFunction(this)" name="paragraph" >Hello World!</p>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.