I have an array of HTML a elements .icon-image-link class with an "Onclick" attribute that contain what appears to be a string (I did not write the HTML code), for example:
onclick="javascript:__doLinkPostBack('','sl~~smartlink||st~~ilslink||su~~Database%3F||sv~~sdbn%253Dcat06260a%2526pbt%253DDatabase%2526issn%253D%2526ttl%253D%25253C%25253C%25253E%25253Etest%252520test%252520test%2526stp%253DC%2526asi%253DY%2526ldc%253D%2526lna%253DDatabases%2526lca%253Dother%2526lo%255Fan','_top');return false;"
I need to check if the Onclick event value contains a substring "pbt%253D + string". This needs to be done for each element in an array (pubtypes). I have written the following code. It is meant to run for each element in putypes, get the index of the current element, then use it to add that element to the substring:
pubtypes.forEach(function(){
var index = pubtypes.indexOf($(this));
$(".icon-image-link").each(function() {
if ($(this).attr('onclick').indexOf('pbt=' + pubtypes[index]) > -1 || $(this).attr('onclick').indexOf('pbt%253D' + pubtypes[index]) > -1) {
This is part of a larger code. I am not getting any error messages on the code, but the code makes no changes to the page, and I suspect the reason is that the if statement returns false every time. I added a log message in the .if statement, but it does not appear in the console, which means the code never got to that message (other log messages before this point appear in the log file, including in side the .each function, after the if statement).
I tried solving the issue by first creating a variable:
var onclickvalue = $(this).getAttribute('onclick');
and
var onclickvalue = $(this).attr('onclick');
then using onclickvalue.contains in the if statement, but that resulted in an error message "getAttribute/attr is not a function".
Aucun commentaire:
Enregistrer un commentaire