/    Sign up×
Community /Pin to ProfileBookmark

Here is a nice trick to replace in <div id="Id01" class="Class01"> direct without JS

Hi everybody,

do you want to include in a <div> or <span> or <p> a id=”ID00″

but this id should be changed to

id=”ID01″
id=”ID02″
id=”ID03″
id=”ID04″
id=”ID05″

or any other id name that you want to change “on the fly” without using document.getelementBy…. ?

I just found a nice trick to do this:

And before you say, this is not for the HTML section.

Believe me, this here is for HTML. But we use PHP to help HTML!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[CODE]
<?php

// This reset the counter for pagination
$FormCounter = 0;

// Lets assume that you got from MySQL the data. You want to show always 5 frames with data.
// The following code works and can be from you used as shown.

while ($row = mysql_fetch_assoc($rs_result))
{
$FormCounter++;
?>

<div class=”ArticleFormDiv” style=”position:relative;background:url(images/DesignSlideshow.jpg);width:673px;height:410px”>

<div style=”position: absolute; top: 0.3em; left: 8.8em; width: 500px;”>

<!– The following line is placing in the src=”” always the picture name which is stored in the MySQL table –>
<img id=”MainPicLink” src=”images/<?php echo $row[“MainPicLink”]; ?>” style=”position: absolute; visibility: visible; left: -6.55em; top: 1.9em; width: 201px; height: 146px; z-index: 200;”>
<!– This is nothing special. –>

<!– The following lines are a example how to show data from the fetched table –>
<p id=”DataItem” align=”left” style=”position: absolute; top: 1.1em; left: 8em; padding: 4px; font-weight: bold; font-size: 18px; color: #625AAE; line-height: 22px;”>
<?php echo $row[“Item”]; ?>
</p>

<p id=”DataItemNo” style=”position: absolute; top: 7.45em; left: 14em; padding: 4px; font-weight: normal; font-size: 16px;”>
<?php echo $row[“ItemNo”]; ?>
</p>
<!– This all is clear. Nothing special. –>

<!– But now comes a problem. What to do if you want to change the id of DataPrice and
add always a auto incremement number? The most people do this in this way or with
document.getElementById and change it.

But what you do if you dont want to call any javascript function?

id=”javascript:NewDataPrice();” does not work.

How you change the id always direct in the field?

id=”I want here to get changed!”

I needed a while to find the following solution. if the auto incrememnt number is a javascript number, convert it to PHP.

Then include the PHP variable as echo in the id.

This is a example that works, but it is not done direct in the field via a place holder.
It is done with a javascript funtion.
–>
<p id=”DataPrice” style=”position: absolute; top: 14.63em; left: 14em; padding: 4px; font-weight: normal; font-size: 16px;”>
<?php echo $row[“Price”]; ?>
</p>

<script type=”text/javascript”>
// Change ID from DataPrice
document.getElementById(“DataPrice”).id = “DataPrice” + jsAutoNumber;
</script>

<!– But what if you dont want to use javascript? Or if you want to replace the id direct with the place holder?
Here comes the solution: Use PHP echo!
First increment the number, put it with the id name together and then echo it.

This works also for replacement of src or classes

Here is a example:
–>
<?php
$php_ID_DataItem = ‘DataItem’ . $FormCounter;
$php_href_DataItem = ‘href_mylink’ . $FormCounter;
$php_class_DataItem = ‘class_myclass’ . $FormCounter;
$php_src_DataItem = ‘images/picname’ . $FormCounter . ‘.jpg’;
?>

<p id=”<?php echo $php_ID_DataItem; ?>” style=”position: absolute; top: 0.87em; left: -7.7em;”><a id=”<?php echo $php_href_DataItem; ?>” name=”DataItemName” href=”javascript:void(0);”><img class=”<?php echo $php_class_DataItem; ?>” src=”<?php echo $php_src_DataItem; ?>” title=”Big Image” width=”644″ height=”380″></a></p>

</div>

</div>

<br>

<script type=”text/javascript”>

// This is the pagination function. It calls always the same page again.
// You should adjust the echo as you need it.
<?php
$sql = “SELECT COUNT(ID) FROM articles”;
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $recpage);

for ($i=1; $i<=$total_pages; $i++)
{
echo “<a href=’body_articles.php?page=”.$i.”‘ class=’body_articles ajaxify’>”.$i.”</a> “;

/* echo “<a href=’pagination.php?page=”.$i.”‘>”.$i.”</a> “; */
};

}?>

</div>
[/CODE]

Even more interesting is to change a serie of CSS parameter. Lets say that you have 20 images, all of them in different areas of the web application. With this code can be all images at the same time displayed or hidden.

[CODE]
<?php

for ($x = 0; $x <= 20; $x++)
{

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@MiaCharlotteSep 27.2016 — Thank you for sharing script for div class.
Copy linkTweet thisAlerts:
@KadarKretschmanSep 27.2016 — nice. its really cam make easy so many things like image handling
Copy linkTweet thisAlerts:
@NogDogSep 27.2016 — Using the "<?" opening PHP tag is not a good idea for code you want to be generally usable, as it is not available in all PHP configurations. "<?php" is safer, as it's always available. (In the most recent versions, the "<?=" tag is always available, too, but not necessarily so in older versions.)
Copy linkTweet thisAlerts:
@jedaisoulSep 27.2016 — I'm keen on using PHP code for things other than database access, but it is potentially misleading to suggest this is HTML. Also, you should mention:

  • 1. You need to have PHP in your web host package (most paid for packages do).


  • 2. The HTML file usually needs to be renamed ".php" instead of ".html".


  • 3. You cannot run PHP in a browser. To test your code locally you need to set up a local host.


  • 4. Unlike HTML, PHP code is executed by the server before the web page is passed to the browser, irrespective of where it is placed in the source file.


  • Thread moved to the PHP forum.
    Copy linkTweet thisAlerts:
    @frankyBangkokauthorSep 28.2016 — Using the "<?" opening PHP tag is not a good idea for code you want to be generally usable, as it is not available in all PHP configurations. "<?php" is safer, as it's always available. (In the most recent versions, the "<?=" tag is always available, too, but not necessarily so in older versions.)[/QUOTE]

    NogDog:

    I know that it is safer to use the word PHP in the opener.

    This here was just fast cooked and published.

    I wanted to share the idea with all my programmer friends, and this includes you and everybody.

    All the people who are in Forums like webdeveloper and others are so helpful. I am not so many times in the blog world, but I know what a great work this people do.

    My way to say thank you is to give ideas out.

    Please, moderator, dont be harsh on me (I dont know where to put this), but this what comes is very special.

    I wrote 20 years ago the first formula for round robin sport leagues with home and away rules.

    This formula is now in more then 5000 leagues around the world used.

    The code is in VB, but can be in any language converted.

    Maybe it is helpful for some people.

    PS.: I like this forum already!

    Here is the code:

    [CODE]Attribute VB_Name = "PairingSystem"
    Option Explicit

    ' This Formula here is for unlimited Round robbin tournaments of any kind with home and away rule.
    ' It works perfect and is used in many leagues.

    ' The Formula is self written from me and can be changed without any problems, if you wish to do it.
    ' The only restriction is that you are not allowed to sell this functions!
    ' This means not that you don't sell your program, but the pairing functions are freeware.
    ' The PairingSystem functions are the property of Franky BaySoft, Bangkok 2002, [email protected]

    ' Example: strPlayday = RoundSystem(18, 7) ..... means 18 players/teams and the 7. Playday

    ' strPlayday is in this case here: 18-13#14-12#15-11#16-10#17-9#1-8#2-7#3-6#4-5#

    'Have fun

    Function RoundSystem(numPlayers, numPlayday)
    Dim strFree As String
    Dim numRounds As Integer
    Dim numOpponent As Integer

    If numPlayers < 2 Then
    MsgBox "at least 3 players needed"
    RoundSystem = ""
    Exit Function
    End If

    If Int(numPlayers / 2) <> numPlayers / 2 Then
    numPlayers = numPlayers + 1
    strFree = "0"
    Else
    strFree = " "
    End If

    numRounds = numPlayers - 1

    If numPlayday > numRounds Or numPlayday = 0 Then
    MsgBox "The playday is not possible"
    RoundSystem = ""
    Exit Function
    End If

    numOpponent = funOpponent(numPlayers, numPlayday)

    RoundSystem = Pairing(numOpponent, numRounds, numPlayday, strFree)

    End Function

    Function funOpponent(numPlayer, numPlayday)
    If Int(numPlayday / 2) <> (numPlayday / 2) Then
    funOpponent = Int(numPlayday / 2) + 1
    Else
    funOpponent = (numPlayer / 2) + (numPlayday / 2)
    End If
    End Function

    Function Pairing(numOpponent, numRounds, numPlayday, strFree)
    Dim numPlayerL, numPlayerR As Integer
    Dim strRound, strRoundTop As String
    Dim i As Integer

    numPlayerL = numOpponent
    numPlayerR = numOpponent

    strRound = ""
    For i = 2 To (numRounds + 1) / 2
    numPlayerL = numPlayerL + 1
    numPlayerR = numPlayerR - 1
    If numPlayerL = numRounds + 1 Then numPlayerL = 1
    If numPlayerR = 0 Then numPlayerR = numRounds
    strRound = strRound & numPlayerL & "-" & numPlayerR & "#"
    Next

    If Int(numPlayday / 2) = (numPlayday / 2) Then
    strRoundTop = numRounds + 1 & "-" & numOpponent & "#"
    If strFree = "0" Then
    strRoundTop = strFree & "-" & numOpponent & "#"
    End If
    Else
    strRoundTop = numOpponent & "-" & numRounds + 1 & "#"
    If strFree = "0" Then
    strRoundTop = numOpponent & "-" & strFree & "#"
    End If
    End If

    Pairing = strRoundTop & strRound

    End Function

    [/CODE]
    Copy linkTweet thisAlerts:
    @jedaisoulSep 28.2016 — 
    I know that it is safer to use the word PHP in the opener.

    This here was just fast cooked and published.

    I wanted to share the idea with all my programmer friends, and this includes you and everybody.
    [/quote]

    Whilst we want to encourage the submission of ideas like yours, it is not helpful if you use obsolete syntax, particularly if you do so knowingly. Can I ask you to give a little more thought and preparation to your posts? I find that it helps to write my posts off-line, including code, and then paste them into the forum when I'm satisfied that they are in good order.

    On a lighter note, can I ask you not to insert carriage returns at the end of each sentence. It makes your posts unnecessarily difficult to read.
    Copy linkTweet thisAlerts:
    @jedaisoulSep 28.2016 — 
    Please, moderator, dont be harsh on me (I dont know where to put this), but this what comes is very special.
    [/QUOTE]

    I do not want to be harsh, and nor do I want to discourage you from submitting ideas. However, we do need you to help us by observing a few simple rules:

  • 1. Please do not put unrelated ideas in the same post, or even the same thread.


  • 2. Instead please start a new thread for each idea.


  • 3. Please make sure that the idea is relevant to this web site.
  • Copy linkTweet thisAlerts:
    @frankyBangkokauthorSep 29.2016 — Ok, I understand it. I will prepare my posts better. I promise it!

    And I will insert CRs.
    ×

    Success!

    Help @frankyBangkok spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...