1

I have an innerHTML declared as follows:

newdiv.innerHTML = '<a href=\'#\' onclick=genGroups('+userid+',\''+coursename+'\','+url+')>'+coursename+'</a>';

I cant send any url and/or string variable(e.g coursename which has spaces in it) through the onclick. The error related to space(incase of string) and url(need a')' after argument) occurs. Any idea?

5
  • 3
    WHY. WHY ARE YOU DOING IT THIS WAY. Commented May 30, 2013 at 20:17
  • 1
    I need to generate a list of courses name onclicking them their corresponding groups will generate in other div. I am actually loading the first div with new divs(dynamically created from database call). Its a popup modal where there are three divs side by side. Any idea? Not interested in JQuery right now. Commented May 30, 2013 at 20:20
  • could you be more precise? you're probably aware that url's can't contain spaces(they become %20)? if that is what you meant? explain a bit more if i'm wrong Commented May 30, 2013 at 20:22
  • no I am getting 2 different errors for two different cases. one for string one for url. Commented May 30, 2013 at 20:23
  • ('+userid+',\''+coursename+'\','+url+') is it right? or should it be: ('+userid+'\','+coursename+'\','+url+') Commented May 30, 2013 at 20:27

4 Answers 4

2

Use the functional method to add a click handler, rather than an inline handler:

newdiv.innerHTML = "<a href='#'>"+coursename+"</a>";

var a = newdiv.getElementsByTagName('a')[0];
a.addEventListener('click', function() { genGroups(userid, coursename, url); });

Also, note the use of different forms of quotes, so you don't have to escape embedded quotes.

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

Comments

2

You could easily do it with jQuery trying something like this http://jsfiddle.net/gianlucaguarini/WSzCh/ that is a lot cleaner and more flexible for your app.

var your_user_id = 1,
    your_course_name = 'whatever',
    your_url = 'nice url';

var genGroups = function(params){
    alert(params.userId);
    alert(params.coursename);
    alert(params.url);
};
// create a new empty link
var $myDiv = $('.myDiv'),
    $newLink = $('<a>');

// to store your meta properties as html data
$newLink.data({
    'userId':your_user_id,
    'coursename':your_course_name,
    'url':your_url
}).text(your_course_name);

// append the new link
$myDiv.append($newLink);


// bind the click event anywhere in your code
$myDiv .on( 'click','a', function() {
    var linkData = $(this).data();
    genGroups( linkData );
});

Comments

1

i think the url should be quoted, too...

and the onclick="..." (with double quotes) would be nice

newdiv.innerHTML = '<a href=\'#\' onclick="genGroups('+userid+',\''+coursename+'\',\''+url+'\')">'+coursename+'</a>';

Comments

0

You have a problem with your quotes. Try this:

newdiv.innerHTML = '<a href="#" onclick="genGroups('+userid+',\''+coursename+'\',\''+url+'\')">'+coursename+'</a>';

I suggest you using unobtrusive JS. You could store userid, coursename and url as data inside the a, and bind a click event. You could even fetch coursename from anchor.

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.