javascript - How to concatenate a JS variable in an HTML script? - Stack Overflow

Can I do something like this?var counter = SomeJSFunctionThatReturnsAvalue();<tr><td> <i

Can I do something like this?

var counter = SomeJSFunctionThatReturnsAvalue();

<tr><td> <input type="file" name="file-upload"+"_counter" id="file-upload" /></tr></td>

Can I do that? I need to append an underscore and an incremented number to the name.

Also, an off-topic question - that function above returns the value of an input type, for example:

<input type="hidden" name="hidden-input-type" value="2" />

Is the value "2" a number that I can use for math operations? If not, how can I make it one?

Can I do something like this?

var counter = SomeJSFunctionThatReturnsAvalue();

<tr><td> <input type="file" name="file-upload"+"_counter" id="file-upload" /></tr></td>

Can I do that? I need to append an underscore and an incremented number to the name.

Also, an off-topic question - that function above returns the value of an input type, for example:

<input type="hidden" name="hidden-input-type" value="2" />

Is the value "2" a number that I can use for math operations? If not, how can I make it one?

Share Improve this question edited Sep 30, 2013 at 2:21 hopper 13.4k7 gold badges51 silver badges53 bronze badges asked Sep 30, 2013 at 1:37 Belmark CadayBelmark Caday 1,6853 gold badges21 silver badges30 bronze badges 1
  • "_counter" is a string. If you want the underscore and the value of counter, you want "_" + counter. – Evan Davis Commented Sep 30, 2013 at 1:46
Add a ment  | 

4 Answers 4

Reset to default 2

Here you go fella.

<head>
<script>
function test($count) {
document.getElementById("test1").setAttribute("name","file-upload_" + $count);
}
</script>
</head>
<body>

<p>some content</p>
<input id="test1" type="file" name="file-upload" id="file-upload" value="2"/>
<p>some other content</p>
<script>test(1);</script>
</body>

Your SomeJSFunctionThatReturnsAvalue(); would pass it to test() function.

to get the value of "2" from your second question for use in a math function, just do:

var value = document.getElementById("test1").getAttribute("value");
document.write(parseInt(value, 10) + 3);

which returns 5.

To append the return value of your function to the name of the input tag, you can assign it to the name attribute of the input.

var counter = SomeJSFunctionThatReturnsAvalue();
var fileUpload = document.getElementById('file-upload');

fileUpload.name = fileUpload.name + "_" + counter;

You can get the type of a variable by using "typeof"

typeof myValue; // "string"

You can change this to an integer by using the parseInt() function.

var intValue = parseInt(myValue, 10);

You can change the name using .setAttribute("name", theNameUwantToChangeTo);:

function changeName(number){

    var ele =  document.getElementById("file-upload");
    ele.setAttribute("name",  b.getAttribute("name")+ "_" + number);
}
changeName(number);

To get the value, just .value:

function getV(){

    return document.getElementById("file-upload").value;
}
var number = getV();

In case it does not return int, use parseInt()

function getV(){

    return parseInt(document.getElementById("file-upload").value);
}
var number = getV();

Maybe you would benefit from looking into Angular.js or Ember.js if you are trying to do things like this. They can do data binding so that you can make readable and dynamic code just like what you are trying to create in your question.

If not that^ then this:

I saw you mentioned in a ment that you are dynamically creating the list. That is where you should be assigning the correct name with the counter (assuming there's no desire for counter to change dynamically. If there is a dynamic change then tell us what events are doing the change) Could you show us the code that is doing that please?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308852a4567850.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信