Solved

prepending a variable in javascript

Posted on 2007-04-06
10
1,501 Views
Last Modified: 2012-05-05
Is there a function out there that will do this:

somevar="something"+somevar;

such as .prepend()? if there isnt, how would imake this function work like this:

somevar.prepend(),

NOT like this though:

prepend(somevar, prepend_value);


If possible, I want both answers so if i ever need to i can alter variables with functions without putting the variable i want to alter in the argument list.
0
Comment
Question by:Oscurochu
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18869564
see JavaScripts prototype syntax and JSON http://www.json.org/
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18869679
Check out the use of the EVAL function.  This function takes a string and executes it, which I believe is what you're looking for.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 18877106
Why over-complicate things? What would this function gain you?

Obviously you know the value to prepend; why not just do as you suggested:

somevar="something"+somevar;
0
 
LVL 5

Author Comment

by:Oscurochu
ID: 18909630
using a function to prepend a variable will clear up a lot of code. i like to keep my code clean, so:


document.getElementById('blah').innerHTML="blah blah"+document.getElementById('blah').innerHTML


makes the code look cluttered. it would be easier to just use:

document.getElementById('blah').innerHTML.prepend('blah blah');
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18909647
In the example you just gave, you really want to insert HTML, so use this function with two argument, the node to be acted on, and the text to be added:

prepend('myNode','blah blah');

function prepend(nd, txt) {
     var curNode = document.getElementById(nd);
     curNode.insertBefore(document.createTextNode(txt),curNode.firstChild);
}

If you aren't sure that there will always be a firstChild node, then modify the function like this:

function prepend(nd, txt) {
     var curNode = document.getElementById(nd);
     if (curNode.firstChild) {
          curNode.insertBefore(document.createTextNode(txt),curNode.firstChild);
     } else {
          curNode.appendChild(document.createTextNode(txt));
     }
}
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Author Comment

by:Oscurochu
ID: 18909706
i already said, i donot want it to work like that because of specific reasons i cant remember right at this moment. im trying to get it to work  like this, if possible:

document.getElementById('blah').innerHTML.prepend('some text');

innerHTML is just an example. if the above function is not possible, then i will just write a function that will use two arguments to do the work.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18910306
as I already said: JSON can do that
0
 
LVL 29

Expert Comment

by:Badotz
ID: 18910802
<$.02>Much ado about nothing</$.02>
0
 
LVL 5

Author Comment

by:Oscurochu
ID: 18911968
i dont understand how JSON works, can you help me out with that?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 20 total points
ID: 18956414
see prototype.js, something like:

String.extend(String.prototype, {prepend: function(str){ /* work here */ });

€.02 as asked for ;-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now