0

I am a little desperate and I need the help of a generous person here!

I have been hired to do a difficult project, but I am only a PHP developer and I know very few about Javascript.

What I need to do is to create a function in Javascript that will be called by an external script ( I do not have this script, so I know only the params the script will pass to the function ). When the function has been called and the params have been passed to the function, this function will start writing some HTML, inside the blocks that the other script has already cloned.

So, the other script will clone the HTML blocks all the times that is necessary and will assign an ID in the object of the cloned HTML block. At this point my function, receives the ID of the cloned HTML block and will start writing inside the cloned block.

This is not clear for me.

This is the example of the code:

function showSubject(params) {
    var targetElement = params.targetElement;
    var subjectName = params.subjectName;
    var numMentions = params.numMentions;
    var numPositive = params.numPositive;
    var numNegative = params.numNegative;
    var imageSrc = params.imageSrc;
    /* write the code in the HTML block */
    return (true or false) //true if all is fine
};

Example of the params my function will get.

var params = {
    candidatesObject: "cloned item",
    candidateName: "Nome Cognome",
    numMentions: 1000,
    numPositive: 1000,
    numInformative: 1000,
    numNegativers: 1000 };

Can anyone give me a direction? An example? Something please... :(

6
  • 2
    Those example params do not make sense. In JavaScript, an object is something along the lines of { a: 1, b: 2 }. Commented Dec 28, 2011 at 14:50
  • @pimvdb: I've corrected the code (just awaiting a peer review) Commented Dec 28, 2011 at 14:58
  • 1
    @JSPerfUnkn0wn You should post that as an answer, not an edit to the question. Commented Dec 28, 2011 at 14:59
  • I'm not sure what's the actual problem here. The params are given by an external application so those are constructed correctly I guess. @DiegoP: Is the actual problem you're facing the writing of content into targetElement? Commented Dec 28, 2011 at 15:00
  • @pimvdb: yes good question, I wasn't too sure about that. Also could you (@DiegoP) confirm that params will be in the form of {key: value} or [value1, value2, ... value_n], and present your question more clearly as I'm having a little trouble understanding what exactly you need help with? Commented Dec 28, 2011 at 15:12

1 Answer 1

2

The easiest way I can think of is:

function showSubject(params) {
    //var targetElement = ...
    //don't bother with all that

    /* write the code in the HTML block */
    var clonedHTML = document.getElementByID( params.targetElement );
    if( !clonedHTML ){
        return false;
    }

    var iDontKnowWhatYouNeedToWrite = "<span style='background:yellow'>"
        + 'subjectName = ' + params.subjectName + "<br />"
        + 'numMentions = ' + params.numMentions + "<br />"
        + 'numPositive = ' + params.numPositive + "<br />"
        + 'numNegative = ' + params.numNegative + "<br />"
        + 'imageSrc = ' + params.imageSrc
        + '</span>';

    clonedHTML.innerHTML = iDontKnowWhatYouNeedToWrite;
    return true;
};

Obviously iDontKnowWhatYouNeedToWrite is whatever you need it to be. Also, wouldn't bother copying the params out into individual variables.

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

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.