charlie_cat
charlie_cat

Reputation: 1850

document.getElementById() problem

I have this line of code:

<a href="#" id="profilelink" name="profilelink" 
    value="<?php echo $id."/".$block_record;?>" 
    onClick="return viewornot(<?php echo $id; ?>)">
   <?php echo $uniqueCode; ?>
</a>

So with the onclick i want to pass the id concatenated with a blocked id to my JS:

function viewornot()
{
    var val = document.getElementById('profilelink');
    var e = confirm('Do you want to view this profile?');

    if (e == true)
    { 
       //for live site
       window.location.href = "http://www.rainbowcode.net/index.php/profiles/showprofilepersonal?id="+id;
       return true;
    }
    else
    {
        //if val contains a 1 then user is blocked
        //display a message then
    }
    return false
}      

the line: var val = document.getElementById('profilelink'); is not giving me what I want it should contain the user id concatenated with a 1(blocked) or 0(not blocked) ($id."/".$blocked_user)

can someone help me please? thank you

Upvotes: 1

Views: 1762

Answers (7)

Pratik
Pratik

Reputation: 30855

In the <a> tag there is no "value" attribute was available you can pass as parameter with another parameter as pass

For example:

<a onclick="view(<?php echo id ?>)</a>

<script>
   function view(id){
         alert(id);
   }
</script>

Try this way

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

document.getElementById('profilelink') returns a reference to the DOM, not the value attribute you have specified (which is invalid HTML).

change that to document.getElementById('profilelink').value (might fail in compliant browsers because value is not a valid attribute for a tag)

or

document.getElementById('profilelink').getAttribute('value');

demo at http://jsfiddle.net/gaby/EWdLD/


Additionally, you most likely want to change the viewornot() definition to viewornot(id) since you seem to pass that as a parameter, and use it inside your method.


Security wise, i hope you do not use this for real security as it is easily bypassed by manipulating the DOM in the client. For more robust security use some server-side code to block/unblock per request.

Upvotes: 4

Jason Small
Jason Small

Reputation: 1054

Ahref can not have a value. Just pass the "val" through the onclick function. "val" will be passed through the function and then you do not need to getElementById

<a href="#" id="profilelink" name="profilelink" onClick="viewornot(<?php echo $id; ?>)"><?php echo $uniqueCode; ?></a>

.

    function viewornot(val)
    {

    var e = confirm('Do you want to view this profile?');

    if (e == true)
    { 
       //for live site
       window.location.href = "http://www.rainbowcode.net/index.php/profiles/showprofilepersonal?id="+id;
       return true;
    }
    else
    {
        //if val contains a 1 then user is blocked
        //display a message then
    }
    return false
}      

Upvotes: 0

Andrew Jackman
Andrew Jackman

Reputation: 13966

If you are looping the link creation script, every link will have an id of profilelink. You can solve this by adding the database id to the end of id in the anchor tag. It would be better to pass everything through the function and not worry about making a variable of the anchor.

Upvotes: 0

Andrew Cooper
Andrew Cooper

Reputation: 32576

a tags don't have a value attribute. You need to pass this value to the function.

<a href="#" id="profilelink" name="profilelink" onClick="return viewornot(<?php echo $block_record; ?>)"><?php echo $uniqueCode; ?></a>

Upvotes: 0

JohnP
JohnP

Reputation: 50019

When you do

var val = document.getElementById('profilelink');

You're actually working with the DOM element that is your link. What you actually want is the parameter that is being passed to your method.

Change your method signature

function viewornot(blockedStatus) {
   var val = blockedStatus;
   //your code
}

You might also want to change your link

<a href="#" id="profilelink" name="profilelink" 
    value="<?php echo $id."/".$block_record;?>" 
  -->   onClick="return viewornot('<?php echo $id."/".$block_record;?>')">
   <?php echo $uniqueCode; ?>
</a>

Upvotes: 1

Christian
Christian

Reputation: 28124

Not to point out the obvious, but val is not the actual value. You need document.getElementById('profilelink').value to do that.

Upvotes: 2

Related Questions