2

Love PHP, but very new to JavaScript. I'm following a tutorial, and one thing has me stuck. I call the markRead function with:

<a href="#" onclick="return false;" onmousedown="markRead(9,notifreq_9)">
function markRead(noteid,elem){
var action = "delete";
var ajax = ajaxObj("POST", "php_parsers/notification_parser.php");
ajax.onreadystatechange = function() {
    if(ajaxReturn(ajax) == true) {
        if(ajax.responseText == "deleted"){
        _(elem).innerHTML = "";
        }
    }
}
ajax.send("action="+action+"&noteid="+noteid);
}

For some reason it is reading noteid as a variable (i can alert(noteid) and it will report 9), but if I try alert(elem) it kicks back [object].

I cheated by assigning elem = notifreq_+noteid, but I would love to understand what is going on here... Can someone point me in the right direction to understand what is happening?

2
  • What is notifreq_9? That is probably an object, where as 9 is just an integer. Commented Nov 12, 2013 at 18:52
  • Is notifreq_9 supposed to be a string? Commented Nov 12, 2013 at 18:52

3 Answers 3

2

you need to use quotes

if you don't use quotes javascript will take it as a object i.e. a variable with that name or id

so change this

onmousedown="markRead(9,notifreq_9)"

to this

onmousedown="markRead(9,'notifreq_9')"
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure you didn't copy - paste this from other answers?
no my internet is too slow ! and who needs to copy the basics of javascript ?? @putvande
@putvande This is a simple enough answer that similar answers are not unexpected. I would have put the same code format and used ' myself, if I had posted an answer.
1

Because it does not have quotes it is being referenced as an object and that goes back to some browsers will look for a variable with that name, if it does not find it it looks for an element with that id.

onmousedown="markRead(9,notifreq_9)">

needs to be

onmousedown="markRead(9,'notifreq_9')">

Comments

1

Anything passed without quotes will be assumed to be a variable, if your trying to pass the actual word, use "markRead(9,'notifreq_9')"

4 Comments

Putting " into the attribute value (without escaping at least) as it is now will cause the attribute value string to end prematurely, and cause other issues. This should probably be pointed out or avoided, particularly given the level of experience OP has indicated in JavaScript.
@ajp15243 -- Thanks for pointing it out, fixed it with single quotes.
Awesome! Thanks a lot guys. I spent 4 hours trying different work arouds... you guys got it in 30 seconds.
@user2067101 Make sure to accept (with the green check/tick mark) the answer that best solves your problem, so that people searching in the future can see that your question was answered.

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.