2

I am trying to change the left padding value of the div class switch-selection in JavaScript. The following from my JavaScript function changes all values.

var ind = 1; //getting index from php variable '1' is just an example

$(".switch-selection").css("paddingLeft","60px");

I need to change only the value of the current index value. I have tried:

$(".switch-selection")[ind].css("paddingLeft","60px");

But that gives me an error. Am I missing something?

3 Answers 3

1

Use eq() to keep it as a jQuery object, since css() is a jQuery method.

$(".switch-selection").eq(ind).css("paddingLeft","60px");
Sign up to request clarification or add additional context in comments.

Comments

1

The other responses addressed the index portion of your question. However, the CSS property for padding on an element's left is padding-left, not paddingLeft. paddingLeft is the Javascript property name.

Comments

0

You could use :eq() selector or .eq() method to specify the index of element :

$(".switch-selection:eq("+ind+")").css("paddingLeft","60px");
//OR
$(".switch-selection").eq(ind).css("paddingLeft","60px");

Hope this helps.

var ind=2;

$(".switch-selection:eq("+ind+")").css("paddingLeft","60px");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="switch-selection">DIV 1</div>
<div class="switch-selection">DIV 2</div>
<div class="switch-selection">DIV 3</div>
<div class="switch-selection">DIV 4</div>
<div class="switch-selection">DIV 5</div>

1 Comment

Is that what you're looking for @ian?

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.