2
 <div class='unsubscribe'><a id='us$id' href='#' onclick='subscribe(u,$id);'>
<img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>

onclick how do i change the first parameter in the onclick function to 's'? So the next time it will look like this.

<div class='unsubscribe'><a id='us$id' href='#' onclick='subscribe(s,$id);'>
<img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>
2
  • 1
    Sorry, what is the question? Or did you just want to inform everyone of your intentions in developing your app that we know nothing about? Commented Aug 22, 2011 at 16:57
  • I would store the state in some variable... changing the onclick attribute through string processing is really not a clean approach. Commented Aug 22, 2011 at 16:59

4 Answers 4

1

I wouldn't do it like you want to. See the XY Problem.

Instead, what you should so, is keep track of the subscription state of the user, either using a cookie or an identifier on the link (data-state="s"), and take notes in the function.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of setting onclick in your HTML do it with Javascript soon after. I'm assuming you're echoing all your code as a double quoted PHP string because of 'us$id':

<div class='unsubscribe'>
    <a id='us$id' href='#'>
        <img src='/unsubscribe.jpg' alt='unsubscribe' />
    </a>
</div>
<script type='text/javascript'>
    document.getElementById('us$id').onclick = function(){
        // Subscribe u
        subscribe(u, $id);

        // Set all future clicks to subscribe s
        this.onclick = function(){
            subscribe(s, $id);
        };
    };
</script>

2 Comments

i've tried this but now nothing seems to be happening? I've checked firebug and nothing happens.
Assuming your PHP is outputting correctly the above code works: jsfiddle.net/Paulpro/YX4Zk Perhaps u and s aren't defined yet when you get to that function.
0

Instead of changing the string in the onclick, you can change it in the javascript itself. This is if you want to change all of the s to u throughout the page (wasn't sure if there was only one or if this is what your intention is).

Remove the first parameter:

<div class='unsubscribe'><a id='us$id' href='#' onclick='subscribe($id);'>
<img src='/unsubscribe.jpg' alt='unsubscribe' /></a></div>

Then change subscribe() to this:

function subscribe(id)
{
    // doing "static" variable in javascript
    if (typeof this.foo == 'undefined')
    {
        this.foo = 's';
    }
    else
    {
        this.foo = 'u';
    }

    ...
}

Comments

0

One way would be to change it textually; use var el = document.getElementById('us$id'); to retrieve the element, then search el.attributes for an attribute with .name value "onclick", and replace its child with a new TextNode with the second function call.

A different approach would be to change the HTML so that subscribe will get both u and s, and somehow (e.g. with a global variable, a static variable, etc.) remember if it's the first or second time that this method is invoked.

1 Comment

-1. Don't recommend on changing the onclick textNode as it can and will cause problems.

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.