How to find an element relative to script's position? For instance, in the case below I would like getSelfParent
to return the $('button')
element
<button>
<script>
getSelfParent().click(function(){...});
</script>
</button>
$(this).parent()
did not work.
EDIT:
I want to avoid:
- adding ID's anywhere
- traversing whole tree every time I am looking for the
self
element
How to find an element relative to script's position? For instance, in the case below I would like getSelfParent
to return the $('button')
element
<button>
<script>
getSelfParent().click(function(){...});
</script>
</button>
$(this).parent()
did not work.
EDIT:
I want to avoid:
- adding ID's anywhere
- traversing whole tree every time I am looking for the
self
element
- @a'r: this is the way I want to create buttons in my php – Jakub M. Commented Sep 13, 2011 at 10:25
- 1 it's very likely that the script will always be inside a certain element so why not just give the element you want to access an ID and access it via $('#id')? – clem Commented Sep 13, 2011 at 10:26
3 Answers
Reset to default 5@Jakub M: this is the way I want to create buttons in my php.
Try to generate HTML|SCRIPT output as:
<button>
<script id='RandomOrUniqueIdValue'>
var script=document.getElementById('RandomOrUniqueIdValue');
// var script=$('#RandomOrUniqueIdValue');
</script>
</button>
I don't think there's really a way of accessing the parent element of a script tag, as a script is executed in reference to the window object, not the html tag it is in.
generally speaking, rather than copying and pasting the same/similar piece of javascript inside each button element, it would make more sense to just give them a class and access them in a single js function
you should do:
<button>
<script type="text/javascript" id="myscriptToHide1">
jQuery('#myscriptToHide1').parents('button').hide();
</script>
</button>