Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - "Selector" for current position in document

Tags:

jquery

Is there a selector or function in JQuery that allows to select the "current position in the document"?

For example if I use

<script>document.write("test");</script>

the output will be placed in the HTML document at the "position" where this "code" stands. But jQuery always requires a special selector. Of course I could be using a "marker div" with a unique selector, but is it also possible to insert HTML with JQuery right at the place where where the JQuery expression stands?

I tried

<script>$(this).after("test");</script>

but this it's not working. Help would be very much appreciated how this can be solved. Thank you!

EDIT:

Regarding the question below: The Idea is not to directly print out something with document.writeln (it was more meant as an example) but to determine the current "position" in the document with jQuery (and than starting from that position on with "navigation/traversal" of elements by using the jQuery functionality.

Maybe this can be called a "relative traversal of elements starting from the current position on in the DOM Tree"?

<div>Position1</div>
<div>Position2</div>
<script> HOW TO GET THIS POSITION WITH JQUERY (SEE NOTES BELOW) TO FURTHER NAVIGATE FROM HERE ON (RELATIVELY) TO THE PREVIOUS OR NEXT ELEMENT</script>
<div>Position3</div>
<div>Position4</div>

NOTES: The script tag with the jQuery statements will be inserted dynamically in the document and what I am interested in is, to be able to navigate from this position on in the document with jQuery. In the script Tag I might call something like "WRAP THE NEXT HTML ELEMENT WITH A TAG VIA JQUERY" or "GIVE ME THE NEXT HTML ELEMENTS VALUE-ATTRIBUTE". As stated above it is possible to insert a "marker div" to determine the position but the question is, if JQuery is "autonomically" able to determine it's position in the DOM-Tree (to start navigation in a "relative manner" from there on).

like image 535
Markus Avatar asked Oct 14 '22 13:10

Markus


1 Answers

$("script:last") seems to work for all modern browsers (except IE)

<html>
<head>
  <title>test</title>
  <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.5.min.js"></script>
</head>
<body>
  <script type="text/javascript">
    document.write($("script:last").text());
  </script>
  <script type="text/javascript">
    document.write("---");
    document.write($("script:last").text());
  </script>
</body>
like image 81
moriyoshi Avatar answered Oct 20 '22 16:10

moriyoshi