0

How do I get the $( data.id ) to become my element? At the moment it evaluates it to an int, not a tag with an specified id. I have tracked it all down to this problem, everything is the correct value according to FireBug until this point.

function editWinner( value, id )
{
    new Ajax.Request( 'ajax_edit_winner.php',
    {
        method: 'get',
        parameters: {
            value: value,
            id: id
        },
        onSuccess: function( response ) {
            var data = response.responseText.evalJSON();
            $( data.id ).setAttribute('onclick', 'editWinner( ' + !data.value + ', ' + data.id + ' )');
            $( data.id ).update( data.value );
        },
        onFailure: function(){ alert('Something went wrong...') }
    });
}

2
  • I'm guessing $ is provided by some library, but there are several that use that variable and they do different things with it. What library are you using? (Oh, and don't setAttribute on event handlers, use something that abstracts addEventListener (and the Microsoft equiv) and do it properly. (Also make use of the CODE formatting button that the Stack Overflow editor provides) Commented Dec 17, 2010 at 15:04
  • I'm using prototype, and sorry for the formatting. How do I do it properly? Commented Dec 17, 2010 at 15:06

1 Answer 1

1

Now I see this is Prototype (sorry) I'd suggest trying either

  • $('' + data.id) i.e. force the ID to be a string before passing it in.
  • $$('#' + data.id) which is Prototype's selector syntax

Depending on your JSON parser the ID might be a string already. Beyond that I'm not sure what to suggest.


Old answer:

To select a tag you need $('#tagId'), i.e. a hash prefix on the ID. Without it's looking for a tag of the given type, e.g. $('div') to find div tags. CSS selector reference

i.e. you probably want $('#' + data.id)

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

4 Comments

Yes, that seems more right, defining the id, but still not working... this is my tag <a onclick="editWinner( false, 264 )" href="#" id="264">Nej (Markera)</a>
You're suggesting jQuery-style syntax here, when the OP is using Prototype
@Gareth oops, you're right, I didn't recognise that as Prototype not jQuery. Take two.
$$( '#' + data.id ) was the way to go! still have to tweak the attribute. Thanks for all help! =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.