0

I need to create a function that will add an attribute to elements that have an attribute with specific values.

$index have values 0-10, Code is working to this point: var element = $("a[data-slide-index*='"i"']");

Firebug gives me:

Blockquote SyntaxError: missing ) after argument list

Rest looks like that:

<script type="text/javascript">

           jQuery(document).ready(function($){ 

                for(var i=0; i<parseInt(<?php echo json_encode($index); ?>);i++){

                  var hoverAtt = "onHover"+i+"()";
                   var element = $("a[data-slide-index*='"+ i +"']");


                 element.next().attr("onmouseover", function(){
                       return hoverAtt;

                   });

                   }
           })
            </script>

There is jFidle example for $index=6: http://jsfiddle.net/Fuh9P/

Edit: I changed concatenation as Sjoerd suggested but still doesn't work.

3
  • JsFiddle won't work for the PHP statement right? Commented Apr 30, 2014 at 9:58
  • You could do a JsFiddle with some sample data your app has outputted though. Commented Apr 30, 2014 at 9:59
  • Be aware that return hoverAtt will return the string "onHover123()", it will not call the function. Commented Apr 30, 2014 at 10:00

1 Answer 1

1

The error message is because you concatenate strings the wrong way. You have this:

var element = $("a[data-slide-index*='"i"']");

Within the $() you try to concatenate three parts, like this:

"a"i"b"

Instead, you should use something like this:

"a" + i + "b"
var element = $("a[data-slide-index*='" + i + "']");
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.