views:

22

answers:

1

I want to check the value of a hidden field triggered by a "h ref onClick" javascript function. If it is"empty", I want to prompt the user to rate my page using a ratings form (thereby staying on the same page and ignoring the h ref target page. If the value is "rated", I want to simply allow the user progress to their intended page.

Here is some code I've developed so far but doesn't really work that well:

function ratings_prompt(){

    var checkIfRated = document.getElementById("hidden_rating");

    if (checkIfRated.value == "empty")
    {
        alert("The Field is set to empty - please rate the form!");
        checkIfRated.value=="rated";
    }
}

Edit: Sorry but I cannot seem to get all the code into the codeblock.

GF

A: 

can't really help out all that much w/out seeing more code and also you didn't really say what the problem is..."doesn't really work that well" is kind of vague...but basically you would have in your link onclick your function call, and you should pass a "this" reference to the function, and you should have return false; in your onclick as well. Then in your function, if the hidden field is not empty, do like location.href = that.href

<a href='somepage.html' onclick="yourFunction(this);return false;">link</a>

<script type='text/javascript'>
function yourFunction(that) {
  if (document.getElementById("hidden_rating").value != "empty") {
    location.href = that.href;
  } else {
    // didn't rate
  }
}
</script>
Crayon Violent
@Crayon,thanks for that. As a newcomer to Javascript, can I ask you this. Does the reference to "this" in the onClick, refer to this href? If so, what am I passing exactly to the "yourFunction(that)" - is it the URL as a string?GF
Grungefreak
you are passing a reference of the anchor object to your function so you can turn around and use the href attribute of it to forward the user. So in other words, you are saying "call this function and here is a reference of this anchor tag to use in your function" and then in your function the that.href is the href attribute of that reference you passed.
Crayon Violent