2

I want to call the function startWorkflow() in line 651 and pass two variables.

But I don't know how to do that. If I am calling like in the line 649, everything works like a charm.

I tried a lot, but I get always errors like

"missing )…." or "...ILLEGAL"

enter image description here

<script type="text/javascript">

$(document).ready(function() {    

    // Rest Call
    var requestUri = "/_api/lists/getbytitle('Marktplatz')/items?$select=Suche_x0020__x002f__x0020_Biete,Kategorie,Beschreibung,Preis,ID,Anbieter,AnbieterEmail,Attachments,AttachmentFiles,Title&$expand=AttachmentFiles&$orderby=ID desc&$filter=Suche_x0020__x002f__x0020_Biete eq 'Suche'";

        $.ajax({
            url: requestUri,
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            success: function (data) {
                $.each(data.d.results, function (i, item){
                    var title = item.Title;
                    var sucheBiete = item.Suche_x0020__x002f__x0020_Biete;
                    var category = item.Kategorie;
                    var description = item.Beschreibung;
                    var price = item.Preis;
                    var id = item.ID;
                    var anbieter = item.Anbieter;
                    var anbieterMail = item.AnbieterEmail;
                    var attachmentUrl = item.AttachmentFiles.results[0];
                    var vorname = anbieter.substr(0,anbieter.indexOf(' ')); 

                    //startWorkflow(id,anbieter);

                    document.getElementById("head" + i).innerHTML = "<a href='#' onClick=startWorkflow(" + id + "," + anbieter + ")><img src='/PublishingImages/icons/delete32_32.png' + alt='' title='löschen'/></a>" + " " + title;               
                    document.getElementById("teamicon" + i).innerHTML = "<a href='#' onClick=openModalDialog('" + attachmentUrl.ServerRelativeUrl + "')><img src=" + attachmentUrl.ServerRelativeUrl + " class='img-responsive hover-effect' alt=" + attachmentUrl.FileName + " /></a>";
                    document.getElementById("body" + i).innerHTML = "Beschreibung: " + "<b>" + sucheBiete + "</b>" + " " + description + "<br>Preis: " + price + " €<br>Fragen an: <a href='mailto:" + anbieterMail + "?Subject=Frage zu " + title + "&body=Hallo " + vorname + ",' target='_top'>" + anbieter + "</a>";
                })
            },
            error: function () {
                alert("Error getting items");
            }                     
        });   

});     

function startWorkflow(itemID,elemAnbieter) {

    var itemAnbieter = elemAnbieter;
    alert("Aber jetzt: " + elemAnbieter);

}

Also tried:

document.getElementById("head" + i).innerHTML = "<a href='#' onClick='startWorkflow("+id+","+anbieter+")'>Click</a>";

returns "missing )..."

And tried:

document.getElementById("head" + i).innerHTML = "<a href='#' onClick=startWorkflow("+id+",'"+anbieter+"')>Click</a>";

document.getElementById("head" + i).innerHTML = "<a href='#' onClick=startWorkflow("+id+',' + "'" + anbieter + "')>Click</a>";

document.getElementById("head" + i).innerHTML = "<a href='#' onClick=startWorkflow(" + id + ",&#39;" + anbieter + "&#39;)>Click</a>";

returns "ILLEGAL"

And tried:

document.getElementById("head" + i).innerHTML = "<a href='#' onClick='startWorkflow(\'"+id+"\', \'"+anbieter+"\')'>Click</a>";

returns "Unexpected token }"

Finally, thanks Dylan Cristy, now it looks like this:

enter image description here

  • Try adding javascript:startWorkflow() – Akhoy Sep 16 '15 at 12:41
  • The workflow is already starting. I think the problem is how I pass the variables in line 651... – Patrick Sep 16 '15 at 12:43
  • Try calling it like this: "<a href='#' onClick=startWorkflow("+id+"','"+anbieter+")> – Akhoy Sep 16 '15 at 12:56
  • updated the picture. Error still the same... – Patrick Sep 16 '15 at 13:00
  • Can you post your code please, that will be more helpful to find a problem. – Asad Refai Sep 16 '15 at 13:01
3

You are using jQuery already, so rather than use the regular onClick, can you take a different approach altogether, and use jQuery to wire up a click event handler? That way you can call your function like you do in line 649.

I'm thinking a different approach like this:

$("#head"+i).click(function(e) {
    startWorkflow(id, anbieter);
    e.preventDefault();
});

You would of course have to handle setting the innerHTML slightly differently too, which really just amounts to not adding the onClick section you are having trouble with. So you could do something like

document.getElementById("head" + i).innerHTML = "<a href='#'><img src='/PublishingImages/icons/delete32_32.png' + alt='' title='löschen'/></a>" + " " + title;

or

$("#head"+i).html("<a href='#'><img src='/PublishingImages/icons/delete32_32.png' + alt='' title='löschen'/></a>" + " " + title);
| improve this answer | |
  • As you might have figguered out already, I am a "script kiddy" and don't have any idea how to handle the innerHTML if I use your suggestion... – Patrick Sep 16 '15 at 14:00
  • @Patrick, updated my answer. – Dylan Cristy Sep 16 '15 at 14:14
  • Dylan Cristy = godlike :) Now I can go home. Thanks dude. Also updated my question with the output! – Patrick Sep 16 '15 at 14:22
0

Your syntax is different from lines 649 and 651.

Take out the ' before and after the comma in 651.

It should look like this:

<a href='#' onClick="javascript: startWorkflow(" + id + "," + anbieter + ");">

EDIT:

Try adding in single quotes after onClick= and at the end.

document.getElementById("head" + i).innerHTML = "<a href='#' onClick='startWorkflow(" + id + " , " + ambieter + ");'>
| improve this answer | |
  • Never mind, looking at your image again that is not the case. – Jordan Sep 16 '15 at 13:07
  • right ;) But thx anyway. Any suggestions is welcome! – Patrick Sep 16 '15 at 13:09
  • Updated with something else to try. – Jordan Sep 16 '15 at 13:19
  • With the line: document.getElementById("head" + i).innerHTML = "<a href='#' onClick='startWorkflow("+id+","+anbieter+")'>CLick</a>"; I get the same error - updated question – Patrick Sep 16 '15 at 13:31
0

"anbieter" and "id" are only valid variables during creation of the html tag. They are not existing, when the "onclick" Event is executed. You still have to set their values into paragraphs if they are strings. so, assuming both are strings (if id is not, then you can leave that paragraph out)

...innerHTML = "<a href='#' onClick='startWorkflow(\'"+id+"\', \'"+anbieter+"\')'>Click</a>";
| improve this answer | |
  • updated question. Returned "Unexpected token }" – Patrick Sep 16 '15 at 13:50
  • There is no "}" in my code. did you debug your code and look which "}" is meant?? – Leopold Lerch Sep 16 '15 at 13:55
  • Well, I still think it's not a problem with my code. Because if I uncomment line 649, it is perfectly working. Also with the other suggested answers I get different error messages... – Patrick Sep 16 '15 at 13:57
  • could you post the html tag, that is created by your code? The main Problem is the stringification of your variables to the onclick-attribute. in line 649 you use the variables that exist in your closure. in linke 651 you create a onclick Attribute in html, which do not have the variables when the user finally clicks on it (they are executed in window, not in the function-closure you define the html-tag). – Leopold Lerch Sep 16 '15 at 14:06
  • the working onclick-attribute must has to be something like "startWorkflow('<valueofid>', '<valueofanbieter>')" otherwise it wont work. However, as the strings of value and anbieter are passed into it, it is of course also possible that they too contains eg "{}" or apostrophes, ... . which will break the JavaScript code like script injection. – Leopold Lerch Sep 16 '15 at 14:09
0

If your function is in a string and you try to pass a parameter other than a number, a apostrophe/quote is probably messing it up.

Try using & #39 ; around the parameters that are not numbers.

Good luck.

"<a href='#' onClick=startWorkflow(" + id + "," + anbieter + ")>

to

"<a href='#' onClick=startWorkflow(" + id + ",&#39;" + anbieter + "&#39;)>
| improve this answer | |
  • updated question - Returned ILLEGAL – Patrick Sep 16 '15 at 14:16
  • Sorry it didn't work for you. This is the problem however I believe. I pass html through strings all the time, and see this error all the time. When the string is read as HTML code I think it sees the value of your variable as an undefined variable itself, and doesn't recognize as a string parameter. Just something to keep in mind in the future in case you can't find a work around like you did this time. – Brian Rood Sep 17 '15 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.