1

I'm appending some html codes on javascript. I'm also binding an onclick to it. The problem is, I'm getting a javascript error each time I press onclick. I'm passing a string to the function onclick. Here's a clearer view.

var propertyTypeDdlValues = "";

propertyTypeDdlValues += "<li onclick='selectPropertyType('Condominium/Studio')'>Condominium/Studio</li>"; 

$("#propertyTypeDdl").html(propertyTypeDdlValues);

This is my selectPropertyType

function selectPropertyType(cattype){
    $("#propertyType").text(cattype);
    $("#hdnPropertyType").val(cattype);
}

I keep on having this error:

Uncaught SyntaxError: Unexpected token } 

I think the problem is how I wrap strings around (" "). What made me say this? Because when I try to just use a simple function like this:

propertyTypeDdlValues += "<li onclick='displayMessage()'>Condominium/Studio</li>"; 

function displayMessage(){
    alert("Message");
}

It goes through the function and the alert message is being displayed.

Any ideas? Thanks!

5
  • 1
    "I'm passing a string to the function onclick" - bad idea. Commented Aug 23, 2013 at 6:30
  • 1
    can u create a js fiddle or post ur html code too Commented Aug 23, 2013 at 6:30
  • Try adding a \ before and after the quotes of Condominium/Studio. Commented Aug 23, 2013 at 6:31
  • @iBlue no need to use txtspk hre. You've got 300 characters per comment - more than enough Commented Aug 23, 2013 at 6:31
  • @JanDvorak aye aye sir :) Commented Aug 23, 2013 at 6:34

5 Answers 5

1

Here is a solution:

JSFiddle

$(function () {  

     $("#propertyTypeDdl").append("<li onclick=
      selectPropertyType('Condominium/Studio')>Condominium/Studio</li>");

     $("#propertyTypeDdl").append("<li onclick=
      'displayMessage()'>Condominium/Studio</li>");
});

function selectPropertyType(cattype) {
    $("#propertyType").val(cattype);
}

function displayMessage() {
    alert("Message");
}
Sign up to request clarification or add additional context in comments.

1 Comment

After reading all answers. This one is the most efficient and effective. It's simple and straightforward. I can't believe I just have to remove " ' " wrapping around onclick. Thanks!
0

One way would be to store that string in a variable and passing that into the function. But I guess some one else will give a better answer.

Comments

0

Modify your propertyTypeDdlValues to

propertyTypeDdlValues += 
   "<li onclick='selectPropertyType('\'Condominium/Studio\'')'>
      Condominium/Studio
   </li>" 

Comments

0

You are using jQuery. So why not:

foreach(thing in listOfThings) {
    var li = $('<li>' + thig.name + '</li>');
    li.click(function(e) {
        selectPropertyType($(this).html());
    });

    $("#propertyTypeDdl").append(li);
}

It's a lot cleaner.

Friendly advice: never ever use HTML on/whatever events again :)

Comments

0

Your original string

"<li onclick='selectPropertyType('Condominium/Studio')'>Condominium/Studio</li>"

Gets parsed as this HTML:

<li onclick='selectPropertyType('Condominium/Studio')'>Condominium/Studio</li>

The onclick atrribute ends just after the left bracket, leading to invalid javascript. Since you have three levels of nesting, you'll need to escape your quotes:

"<li onclick='selectPropertyType(\'Condominium/Studio\')'>Condominium/Studio</li>"

or

"<li onclick='selectPropertyType(\"Condominium/Studio\")'>Condominium/Studio</li>"

but it is better to avoid pieces of javascript code as strings (and, since you're using jQuery, avoid onclick attributes altogether), and create an element and set its click handler to a function. This will also avoid the need to nest string literals:

var $li = $("<li>").text('Condominium/Studio').click(function(){
  selectPropertyType('Condominium/Studio')
});

$propertyTypeDdlValues = $propertyTypeDdlValues.add($li)

Comments

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.