×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    Problem with onclick

    i'm parsing a URL and creating DIV as needed.

    function parseView(view, parentDiv)
    {
    var viewName = view.getAttribute("name");

    if (viewName == "scroll_view") {
    var subViews = view.childNodes;
    for (i = 0; i < subViews.length; i++)
    parseView(subViews.item(i), parentDiv)
    }
    if (viewName == "story") {
    var StoryDiv = document.createElement("div");
    var storyId = parentDiv.getAttribute("id") + "Story" + storyCounter++;
    StoryDiv.id = storyId;
    parentDiv.appendChild(StoryDiv);
    var imgDiv = document.createElement("img");
    var titleDiv = document.createElement("div");
    var synDiv = document.createElement("div");
    var imageId = StoryDiv.getAttribute("id") + "_image";
    imgDiv.id = imageId;
    var titleId = StoryDiv.getAttribute("id") + "_title";
    titleDiv.id = titleId;
    var synId = StoryDiv.getAttribute("id") + "_synopsis";
    synDiv.id = synId;
    if (view.childNodes[1].getAttribute("src") != "") {
    var img = view.childNodes[1].getAttribute("src");
    var start = img.search(/=/);
    var end = img.search(/&/);
    img = img.substring(start + 1, end);
    imgDiv.setAttribute('class', 'story_image');
    imgDiv.setAttribute("src", img);
    StoryDiv.appendChild(imgDiv);
    }
    titleDiv.textContent = view.childNodes[0].textContent;
    titleDiv.setAttribute('class', 'story_title');
    StoryDiv.appendChild(titleDiv);
    var substr = view.childNodes[3].textContent;
    substr = substr.substring(0, 150);
    substr = substr + ".....";
    synDiv.textContent = substr;
    synDiv.setAttribute('class', 'story_synopsis');
    StoryDiv.appendChild(synDiv);
    StoryDiv.onclick = fullStory;
    }
    }
    StoryDiv.onclick = fullStory; works perfectly fine but when i give parameter to that function
    StoryDiv.onclick = fullStory(id,url); it automatically gets called without even clicking.

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: Problem with onclick

    Hi Daaku,

    in your code, the first version:
    Code:
    StoryDiv.onclick = fullStory;
    is the correct one, since you're assigning a function as an event handler.

    The second one instead executes the function and then assign its return value to the 'onclick' property:
    Code:
    StoryDiv.onclick = fullStory(id,url);
    In your case, you could define an anonymous function, and some local variables to be used as arguments of this function. E.g.:
    Code:
    var id = "the id you want to pass to fullStory";
    var url = "the URL you want to pass to fullStory";
    
    StoryDiv.onclick = function()
    {
      //let's call your fullStory() method
      //with the variables defined above
    
      fullStory(id, url);
    }
    Hope it helps,
    Pit

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    30

    Re: Problem with onclick

    got the point thanks... the problem is that i'm creating StoryDiv dynamically and thr's more than one StoryDiv and different url's for those different StoryDiv.


    var id = "the id you want to pass to fullStory";
    var url = "the URL you want to pass to fullStory";

    StoryDiv.onclick = function()
    {
    //let's call your fullStory() method
    //with the variables defined above

    fullStory(id, url);
    }

    Now in the function StoryDiv.onclick = function() how will i come to know which StoryDiv generated the click event if i am not passing it to the function fullStory(which i can not).

Similar Threads

  1. STLPort namespace issue
    By yickhong in forum Open C/C++
    Replies: 0
    Last Post: 2009-04-09, 08:54
  2. SyncML link problem
    By samuel_ in forum Symbian
    Replies: 4
    Last Post: 2009-03-27, 08:02
  3. Problem with eglSwapBuffers and heap corruption
    By greatape in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2007-05-24, 03:35
  4. netcards - Problem with opening adapter
    By kernj in forum Symbian Tools & SDKs
    Replies: 5
    Last Post: 2007-01-10, 08:56
  5. Problem: S60 SDK for CW in PC with .NET
    By anttij in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2005-02-28, 11:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×