Reputation: 395
I have some div i want change selective div position to center means suppose I select the first two div only that div text move to center
on click
$("#key").click(function myfunction() {
$("div").css("text-align", center);
});
div {
background-color: yellow;
margin-top: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="key">clickme</button>
<div onselect="myfunction()">blabla</div>
<div onselect="myfunction()">cat</div>
<div onselect="myfunction()">rose</div>
Upvotes: 1
Views: 89
Reputation: 1507
@aswathy
your all code is okay, only you missed "text-align", 'center'
center
property should be in single quotes
$("#key").click(function myfunction() {
$("div").css("text-align", 'center');
});
div {
background-color: yellow;
margin-top: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="key">clickme</button>
<div onselect="myfunction()">blabla</div>
<div onselect="myfunction()">cat</div>
<div onselect="myfunction()">rose</div>
Upvotes: 0
Reputation: 181
Just a minor miss, need to assign value as string as shown below:
$("#key").click(function() {
$("div").css("text-align", "center");
});
If you want to give margin than also number will be specified as string as:
$("#key").click(function() {
$("div").css("padding", "50");
});
Upvotes: 0
Reputation: 36609
toggle
) selected
class on click
of elementinline
event onselect
center
property to only those elements which are having selected
classquotes
, $("div").css("text-align", 'center');
$("#key").click(function myfunction() {
$("div.toGrab:not(.selected)").css("text-align", '');
$("div.selected").css("text-align", 'center');
});
$("div.toGrab").on("click", function() {
$(this).toggleClass('selected');
});
div {
background-color: yellow;
margin-top: 20px;
cursor: pointer;
}
.selected {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="key">clickme</button>
<div class='toGrab'>blabla</div>
<div class='toGrab'>cat</div>
<div class='toGrab'>rose</div>
Upvotes: 0
Reputation: 29683
You don't need to give name to click function
. It can be anonymous. Also it should be $("div").css("text-align", 'center')
to assign a css
property.
function myfunction() {
//Not sure why you have this on div.
}
$(document).ready(function() {
$("#key").click(function() {
$("div").css("text-align", 'center')
});
});
div {
background-color: yellow;
margin-top: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="key">clickme</button>
<div onselect="myfunction()">blabla</div>
<div onselect="myfunction()">cat</div>
<div onselect="myfunction()">rose</div>
Upvotes: 1