简体   繁体   中英

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

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.

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

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>

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>

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

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.

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
}      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM