Dpkg Me
Dpkg Me

Reputation: 99

Concatenating # to make an id

I have the following codes which append the following html dynamically to my user interface.

<a href =\'#\' onClick=\'scroll('+data.cid+')\'>comment</a>

The data.cid is retrieved via pusher.com and is an integer valye, say 428.

In the scroll function, i will need to concatenate a hashtag in front of data.cid to make it scroll to the div #428

My scroll function i basically this.

function scroll(cid) {

    var id = "#" + cid; 
            $.scrollTo(id) ; 

 }

which should be equivalent to:

$.scrollTo('#428') ; 

except its not and it is not scrolling to the required div. Everything works fine if i hardcode #428 in the function like above.

Clearly there is an issue with concatenating a hashtag in front of the numerical value. Is this the right way to do it?

Upvotes: 1

Views: 91

Answers (1)

James Hill
James Hill

Reputation: 61832

You JS looks fine. You markup is messed up. See comments below:

<a href =\'#\' id =onClick=\'scroll('+data.cid+')\'>comment</a>
               ^ id=onclick=...?

If the markup change does not fix your issue, use console.log to check the value of cid inside scroll(). Chances are it's not being passed in properly. Your JS is fine as is.

Upvotes: 2

Related Questions