1

This code works fine to send one parameter to a javascript function, but when I add a second parameter, it doesn't. onclick="open_livemass(\"Devotion\",'+devotion_channel.idx+');". Is this the right way to send these parameters to the function? One parameter is static. The other, devotion_channel.idx, is determined by the javascript that creates the HTML.

show_devotion_alert = '<span style="cursor:pointer;"'+
  'onclick="open_livemass(\"Devotion\",'+devotion_channel.idx+');"><br/>'+
  devotion_name+' '+time_info+'<br/>('+devotion_channel.name+')</span>';

$('#Devotion_alert').html(show_devotion_alert);

4 Answers 4

2

You're mixing quote styles

'onclick="open_livemass(\'Devotion\',' + devotion_channel.idx + ');"

But why not do this:

show_devotion_alert = '<span id="newSP" style="cursor:pointer;"><br/>'+
  devotion_name+' '+time_info+'<br/>('+devotion_channel.name+')</span>';

$('#Devotion_alert').html(show_devotion_alert);

$("#newSP").click(function() {
    open_livemass("Devotion", devotion_channel.idx);
});

Or better yet:

show_devotion_alert = $('<span />').css("cursor", "pointer").html("<br/>" +
  devotion_name + ' ' + time_info + '<br/>(' + devotion_channel.name + ')');

show_devotion_alert.click(function() {
    open_livemass("Devotion", devotion_channel.idx);
});

$('#Devotion_alert').html(show_devotion_alert);
Sign up to request clarification or add additional context in comments.

3 Comments

Direct event binding is always better than encoding an onclick attribute in the HTML string.
Looks like the problem was the , was in the wrong place in the quotes. Several other people had suggestions about using the click event. That makes sense.
@user823527 - yeah - trying to insert click handlers like that is painful -- jQuery makes it much easier.
1
$('#Devotion_alert').empty().append( $( "<span>", {
    css: {
        cursor: "pointer"
    },

    html: "<br />" + devotion_name + " " + time_info + "<br />",

    click: $.proxy( open_livemass, this, "Devotion", devotion_channel.idx )
}));

1 Comment

Good tip for having css, html and event neatly specified when creating new elements.
0

Try this:

show_devotion_alert = "<span style=\"cursor:pointer;\" onclick=\"open_livemass(\"Devotion\","+devotion_channel.idx+");\"><br />"+devotion_name+" "+time_info+"<br />("+devotion_channel.name+")</span>";

Comments

0

I would suggest you to try this.

var show_devotion_alert = '<span style="cursor:pointer;"><br />' 
                        + devotion_name + ' ' + time_info + '<br />('
                        + devotion_channel.name + ')<span/>';


$('#Devotion_alert').html(show_devotion_alert)
.find('span')
.click(function(){
    open_livemass('Devotion', devotion_channel.idx);
});

1 Comment

Looks like specifying the click separately is a good way to fix the problem.

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.