Attach a button action to the div it's inside?
Edit What I'm actually asking if I can target the div my button is inside without knowing the name of the id?
I want to write a javascript function that will change the properties of an INPUT box and the button itself, but I want to do this without creating a unique id for each button and each input box.
Let's say I have an onclick javascript like this
function changeid ()
{
var e = document.getElementById("changebutton");
e.id = "savebutton";
$('#TheAttachedDiv').attr('readonly', false);
}
function changeid ()
{
var e = document.getElementById("savebutton");
e.id = "changebutton";
$('#TheAttachedDiv').attr('readonly', true);
}
Because I will have 4 fields with the same class for other reasons, I don't want the properties of all CHANGE buttons and DIVS changed when I click the button besides one div.
Is there a way to do this? Th开发者_JAVA技巧anks
What I'm working with:
<div class="question active">Q1
<input id="one" type="text">
</div>
<div class="button passive">
<input type="button" value="Change" id="changeone" onclick="changeid()">
</div>
The same div repeats on and on.
Try the jQuery method .parent()
if you want the immediate parent or .parents( ... )
if you want to search for ancestors higher up the DOM tree.
Should be something like
jQuery('.field_same_class').click(function(){
if(jQuery(this).attr('readonly')==false)
{
jQuery(this).attr('readonly',true);
jQuery(this).next('.button_class').attr('readonly',true);
} else {
jQuery(this).attr('readonly',false);
jQuery(this).next('.button_class').attr('readonly',false);
}
});
精彩评论