0

My JavaScript function works fine, but I have problems getting different ids from the PHP input box.

JavaScript

window.onload = function()
{   
new JsDatePick({
useMode:2,
target:"inputField1", //HERE I WOULD LIKE TO PASS DIFFERENT ID ex. "inputField1"+ "i"
dateFormat:"%Y-%M-%d",
yearsRange:[1978,2120],
limitToToday:false,
cellColorScheme:"beige",
imgPath:"main/img/",
weekStartDay:1
});

My PHP input box for loop

<div class = "start_date" >
<strong><label for="start_date">Start Date</label></strong>
<br/><br/> 
<?php
for($k=1;$k<=$textboxindex;$k++)
{
echo "<input type=\"text\" class='textboxsize' id= \"inputField1\"   name=\"start_date[]\" value=\"$start_date\" />";
echo "<br/>";
 }
?>
</div>

It works fine, but I would like to have different ID names to use in the JavaScript function. Any ideas?

This doesn't work...

echo "<input type=\"text\" class='textboxsize' id= \"inputField+$k\" name=\"start_date[]\" value=\"$start_date\" />";

Any help will be appreciated.

2
  • You don't need to put the + sign to concatenate string within double quotes (it's dot, by the way). What exactly doesn't work in the second code sample? Commented Oct 30, 2012 at 18:14
  • I have multiple input box but the date function is works for only 1 input box because of the ID Commented Oct 30, 2012 at 18:17

3 Answers 3

1

It's most likely with JsDatePick widget. Its target parameter takes a single ID of an element, therefore you'd have to wrap the JS code in a loop and initiate a separate instance of the widget for each field ID.

Assuming your input field indexing starts with 1:

window.onload = function()
{   
    var i = <?=$totalNumberOfInputs;?>

    for(j=1;j<=i;j++) {  
        new JsDatePick({
            useMode:2,
            target:"inputField" + j, //HERE I WOULD LIKE TO PASS DIFFERENT ID ex. "inputField1" + j
            dateFormat:"%Y-%M-%d",
            yearsRange:[1978,2120],
            limitToToday:false,
            cellColorScheme:"beige",
            imgPath:"main/img/",
            weekStartDay:1
        });
    }
}

You don't need to put the + sign to concatenate strings within double quotes (it's dot, by the way).

Sign up to request clarification or add additional context in comments.

2 Comments

this is the answer for his other question to be precise. there is an error in the code btw. you wanna fix that up.
While not explicitly stating it, author is trying to enable a date picker on multiple input fields. He had a surplus sign in his input field definition AND an onLoad code to be fixed. At least that's my understanding, judging from both this and his next question. Perhaps the question should be rephrased.
1

Change:

id= \"inputField+$k\" name=...

To:

id=\"inputfield$k\" name=...

What is screwing it up is the "+" sign. PHP uses "." to concatenate strings. ECHO out $k properly and you shouldn't have any trouble

7 Comments

Why did you accept this one as an answer, while it doesn't fix your code at all?
@bth you missed the question. the main question was It works fine, but I would like to have different ID names to use in the JavaScript function
@itachi, not really, given his next question: stackoverflow.com/questions/13145639/…
@bth that's a javascript's question. Nothing related with this question.
@bth I guess I don't see how what I posted "doesn't fix the code at all". I posted a solution to what I thought the end result should be and it appears to be an obvious solution.
|
0
 //this is doesn't work 
echo "<input type=\"text\" class='textboxsize' id= \"inputField$k\" name=\"start_date[]\" value=\"$start_date\" />";

just remove that + sign.

2 Comments

thanks 1 more question How can i use for loop in JavaScript for increment ID
javascript has a for loop too. identical syntax as that of php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.